-3
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int randnum = 0;
        boolean lose = false;
        Random gen = new Random();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter desired numbers to be drawn");
        int print = scan.nextInt();
        System.out.println("Enter desired numbers on dice");
        int dice = scan.nextInt();
        System.out.println("Enter your lucky numbers");
        int[] numbers = new int[print]; // int and print rhyme 
        for(int i=0; i<print;i++){
            numbers[i] = scan.nextInt();
        }
        for(int counter=1; counter<=print;counter++){
            randnum = 1+gen.nextInt(dice);
            System.out.println(randnum + " ");
            System.out.println();
            if (randnum == numbers[counter - 1]){
                lose = false;
            } else {
                lose = true;
            }
            if (lose == true){
                System.out.println("Bad luck!");
            }
            if (lose == false){
                System.out.println("Winner winner chicken dinner!");
            }
        }
    }
}

I am making a simple lotto game. If I correctly guess the numbers I will still get the losing outcome unless it was the last number I guessed. How do I compare randnum to all numbers that were inputted?

mmking
  • 1,564
  • 4
  • 26
  • 36
  • 4
    What's your code supposed to do? What's your question? What output are you expecting? What output are you getting? What errors are there? What have you tried? What happened when you debugged? – Kon May 21 '15 at 15:32
  • The progrm is supposed to display the winning message if *one* of the guesses are correct, or if *all* of the guesses are correct? – mmking May 21 '15 at 15:39
  • If at least one is correct then you are supposed to get the winning message but when I run the code you can only win if you get the last number you put. – SouR Graphics May 21 '15 at 15:42
  • So you generate `print` number of random numbers, and if one of your guesses matches one of the random numbers, display the winning message. Is that correct? Or do you just want one random number that you try to guess? – mmking May 21 '15 at 15:44
  • Yes, if one of your guesses match you win. – SouR Graphics May 21 '15 at 15:46
  • Well one of the statements will be printed for each number, since you're inside the loop, so you're going so see Bad Luck! for all numbers that don't match even if another one matches. – JP Moresmau May 21 '15 at 15:49
  • If you enter 1 for numbers to be drawn, numbers on dice and your lucky number you will get the winning outcome – SouR Graphics May 21 '15 at 15:52

1 Answers1

0

If you only want one randnum, take the definition for randnum out of the for loop. You can break out of your loop as soon as you find a match.

randnum = 1 + gen.nextInt(dice);
System.out.println(randnum + " ");
System.out.println();
for(int counter=1; counter <= print; counter++){
    if (randnum == numbers[counter - 1]){
        lose = false;
        break;
    } else {
        lose = true;
    }
}

If you want multiple randnums, you need two loops.

for(int counter = 1; counter <= print; counter++){
    randnum = 1 + gen.nextInt(dice);
    System.out.println(randnum + " ");
    System.out.println();

    for(int i = 1; i <= print; i++){
        if (lose == false){
            break;
        }
        if (randnum == numbers[i - 1]){
            lose = false;
            break;
        } else {
            lose = true;
        }
    }
}

Make sure to initialize lose as true;

boolean lose = true;

You need to take the rest of the if statements out of the for loop.

if (lose == true){
    System.out.println("Bad luck!");
}
if (lose == false){
    System.out.println("Winner winner chicken dinner!");
}
mmking
  • 1,564
  • 4
  • 26
  • 36
  • I commented before: do you want one `randnum`, or do you want `print` number of `randnum`? So if `print` is 4, do you want to compare the inputted values to one `randnum`, or to four different `randnums`? – mmking May 21 '15 at 16:03
  • Yeah I see now. I want 4 different randnums – SouR Graphics May 21 '15 at 16:05
  • Then you'll have to use the second one, with two loops. – mmking May 21 '15 at 16:06
  • It's inconsistent. I guess the right number and sometimes I get winning outcome and sometimes I get losing outcome – SouR Graphics May 21 '15 at 16:13
  • It works for me. Can you give me the numbers? Like what you input for `print`, `dice`, and `numbers`? And what were the random numbers? – mmking May 21 '15 at 16:21
  • Entered 3 for drawn numbers, entered 4 for numbers on dice and lucky numbers were 1, 2 and 3. results were (in order): 3, 3, 4 Bad luck! – SouR Graphics May 21 '15 at 16:24
  • I've made an edit. Add an if statement before checking the inputted values so if the correct value is false, it stays false. If there are any other problems, just let me know. – mmking May 21 '15 at 17:03
  • It seems to be consistent. How did the correct value change from false to true? – SouR Graphics May 21 '15 at 18:11
  • You mean in the previous version? The break keyword only broke out of the inner `for` loop. See [here](http://stackoverflow.com/questions/12393231/). If the final `randnum` didn't match, you get the "Bad luck" message. – mmking May 21 '15 at 18:14