java - Stuck with looping an if statement from within itself -


i doing slot machine-like project. i've ran problem of needing loop if statement, if user input yes, within loop? here example code of trying 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 3 match - jackpot");     system.out.printf("would 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 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 code, can see within if statement, trying run if statement when user's input = y (for yes) if user enter y when prompted, if statement loop.

the outcomes stated, if 3 number match, output: if 2 numbers match, output: if no numbers match output:

i hope understand

i'd suggest do while construct.

scanner scan = new scanner(system.in);   random numrand = new random();  boolean keep_playing = true;  {     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 3 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 play again? ");      string input = scan.nextline();     keep_playing = input.equals("y");  } while ( keep_playing )  scan.close(); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -