I am trying to compare an array user_correct[] if it has a 0 it hasnt been used if it has a 1 it has. However it is not registering my if statements.

import java.util.Random;
import java.io.*;

public class Lottery
{

	public static void main (String[] args) throws IOException
	{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
		int user_nums[] = new int[3];
		int winner[] = new int[3];

		int user_correct[] = new int[3];
		int choice=0;
		int num_correct =0;
		int numsets=0;
		Random generator = new Random();

		System.out.println("Welcome to Ken's Programming Education Lottery!");
		do{
			System.out.println("Please select an option:");
			System.out.println("1 - Choose my own lottery numbers");
			System.out.println("2 - Have the computer generate my numbers");
			choice= Integer.parseInt(in.readLine());

			//fills in the ticket numbers if user wants to pick them
			 if (choice==1)
				{
					for (int i=1; i<4; i++){
					do{
						System.out.println("Please enter a value between 1 and 50 for number " + i + " on your ticket:");
						user_nums[i-1]=Integer.parseInt(in.readLine());
					}
					while(user_nums[i-1]>50 || user_nums[i-1]<1);
					}

				}else if (choice==2){
					for (int i=0; i<3; i++){
						user_nums[i]=generator.nextInt(50)+1;
					}
				}
		}while(choice!=1 && choice!=2);
		// do while to get out when the lottery ticket is a total winner
		do{
			for (int i =0; i<3; i++){
				winner[i]=generator.nextInt(50)+1;
				user_correct[i]=0;
			}

			num_correct=0;
			numsets++;
			//loop to compare the current winning numbers and the user picked numbers
			for (int j=0; j<3; j++){
				for (int k=0; k<3; k++){
					if (user_nums[k]==winner[j]){
						if (user_correct[k]==0){
							user_correct[k]=1;
							num_correct++;
						//	if (num_correct==2){
								//for (int u =0; u<3; u++){
								//	System.out.print (""+user_correct[u]+" ");
								//	System.out.print (""+winner[u]+" ");
						//		}
							//}
						}
					}
				}
			}
		}while(num_correct <3);
		System.out.println("Your lottery numbers are:" + user_nums[0] + " " + user_nums[1] + " " + user_nums[2] + " "/* + user_nums[3] + " " + user_nums[4] +""*/);
		System.out.println("Lottery numbers were:" + winner[0] + " " + winner[1] + " " + winner[2] + " "/* + winner[3] + " " + winner[4] +""*/);
		System.out.println("Winner!It took " + numsets + " tries to win the lottery!");
	}
}

Recommended Answers

All 3 Replies

>I am trying to compare an array user_correct

if(user_correct[0]==0 && user_correct[1]==0 && user_correct[2]==0)
   {  ... }

That doesnt seem to be the problem i dont think. I think the error is in the if checking if user_correct is the 0 or 1.

The real solution to this problem would be to use a boolean variable hasBeenUsed and set it to false initially. Then once your array is used, set it to true. That way you can check the variable later to see if it was ever used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.