Friday, 27 September 2013

Random numbers in java does not work properly

Random numbers in java does not work properly

I am new to java and in the learning phase.I want to create a lottery
program that creates four random numbers, each between 0 and
9(inclusive).This program asks user to guess four numbers and compares
each user guesses to four random numbers and displays the won message as:
No matches 0 points Any one digit matching 5 points Any two digits
matching 100 points Any three digits matching 2,000 points All four digits
matching 1,000,000 points
My program runs but it has some logic errors.
For example,the output should be:
Random numbers:2 3 3 4
Guess numbers: 1 2 5 7-->1 matching digit
Guess numbers: 3 5 7 3-->2 matching digits
Guess numbers: 3 3 3 1-->2 matching digits
Guess numbers: 3 3 3 3-->2 matching digits
**public class Lottery
{
public static void main(String[] args) {
final int LIMIT=10;
int totalCount=0;
int totalPoint;
Random random=new Random(); //creating object of random class
Scanner input=new Scanner(System.in);//creating object of scanner
class
//declaring two arrays
int[] guessNumber= new int[4];
int[] randomNumber=new int[4];
for(int i=0;i<4;i++)
{
randomNumber[i]=random.nextInt(LIMIT);//returns value between 0 to
9(inclusive)
}
for(int i=0;i<4;i++)
{
System.out.println("Enter your first guess number from 0 to 9:");
guessNumber[i]=input.nextInt();
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (randomNumber[i] == guessNumber[j])
{
++totalCount;
break;
}
}
}
if(totalCount == 1)
{
totalPoint=5;
}
else if(totalCount == 2)
{
totalPoint=100;
}
else if(totalCount == 3)
{
totalPoint=2000;
}
else if(totalCount == 4)
{
totalPoint=100000;
}
else{
totalPoint=0;}
//dispalying points
System.out.println("You have earned " +totalPoint+ "points!!");
}
}**

No comments:

Post a Comment