0

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

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
SeanHub
  • 5
  • 7
  • 1
    look into `while` loop: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – denvercoder9 Nov 10 '16 at 17:55
  • 1
    Also, look into `break` statement. http://stackoverflow.com/questions/7951690/how-do-i-exit-a-while-loop-in-java – denvercoder9 Nov 10 '16 at 17:57
  • 1
    To further clarify on @RafiduzzamanSonnet comment, learn a bit about do...while loops. Start simple, for example, try prompting a user for a number until they enter 9. Also, think outside of your if statement, is it really required, or could you somehow use it as a loop condition? Good luck! – Stefan Nov 10 '16 at 17:58

2 Answers2

1

1. Recursion

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.

2. While loop

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
Community
  • 1
  • 1
Ivan
  • 3,781
  • 16
  • 20
  • 1
    You can also use a Boolean variable instead of an infinite loop, some people find that easier to read. – jthort Nov 10 '16 at 18:02
  • Using your recursion method did help big time, but i still ran into some problems at a later date with the looping working the way i wanted it to! Thank you lots though :) – SeanHub Nov 10 '16 at 18:33
1

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();
Michael
  • 41,989
  • 11
  • 82
  • 128