Reputation: 5
I am currently doing a slot machine-like project. I've ran into the problem of needing to loop an IF statement, but only if the user input is YES, within that loop? Here is some example code of what I am trying to do.
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner scan = new Scanner(System.in);
Random numRand = new Random();
num1 = numRand.nextInt((9 - 0) + 1);
num2 = numRand.nextInt((9 - 0) + 1);
num3 = numRand.nextInt((9 - 0) + 1);
System.out.println(num1 + " " + num2 + " " + num3);
if(num1 == num2 && num1 == num3 && num2 == num3) {
System.out.println("All three match - jackpot");
System.out.printf("Would you like to play again? ");
String Yes = scan.nextLine();
if(Yes.equals("y")) {
}
String No = scan.nextLine();
if(No.equals("n")) {
}
}
else if(num1 == num2 || num2 == num3 || num1 == num3) {
System.out.println("Two number match");
System.out.printf("Would you like to play again? ");
String Yes = scan.nextLine();
if(Yes.equals("y")) {
}
String No = scan.nextLine();
if(No.equals("n")) {
}
}
else {
System.out.println("No numbers match");
}
scan.close();
From my code, you can see that within the IF statement, I am trying to run another if statement for when the user's input = Y (for yes) this is so that if the user were to enter y when prompted, then the if statement would loop.
The outcomes are stated, that if all 3 number match, then output: if 2 numbers match, output: if no numbers match then output:
I hope you understand
Upvotes: 0
Views: 737
Reputation: 44250
I'd suggest a do while
construct.
Scanner scan = new Scanner(System.in);
Random numRand = new Random();
boolean keep_playing = true;
do
{
num1 = numRand.nextInt((9 - 0) + 1);
num2 = numRand.nextInt((9 - 0) + 1);
num3 = numRand.nextInt((9 - 0) + 1);
System.out.println(num1 + " " + num2 + " " + num3);
if(num1 == num2 && num1 == num3) { // && num2 == num3 - unnecessary
System.out.println("All three match - jackpot");
}
else if(num1 == num2 || num2 == num3 || num1 == num3) {
System.out.println("Two number match");
}
else {
System.out.println("No numbers match");
}
System.out.printf("Would you like to play again? ");
String input = scan.nextLine();
keep_playing = input.equals("y");
} while ( keep_playing )
scan.close();
Upvotes: 1
Reputation: 3836
There are two ways to have this, one thing is to handle it via recursion. E.g. if all of this code lives in a function with this signature
public static void main(String[] args)
you can just add a call to it, e.g.
if(Yes.equals("y")) {
main(new String[0]); // String[0] is just to satisfy the arguments of the main function, if yours requires other arguments, put them there
}
Downside of this is that your code will create Scanner etc. Also at some point you will see exception as you will run out of stack.
Another option is to put all of your code into a while
construction and use continue
to perform all of the code inside once more and break
to leave the loop:
while (true) {
// S: loop start
num1 = numRand.nextInt((9 - 0) + 1);
. . .
if(Yes.equals("y")) {
continue; // this will cause us to go to S
} else {
break; // this will cause us to go to A
}
}
// A: after loop
Upvotes: 1