java - Exit strategy doesn't work -


i'm trying make exit strategy in small game. when enter yes runs te script again when finish script doesn't ask:

do want try again

but starts again without using exit strategy.

how need make exit strategy working when it's running second time?

package steenpapierschaar1;   public class steenpapierschaar1 {      public static void main(string[] args) {          final int rock = 1 ;   // waarde van rock         final int paper = 2;  // waarde van paper         final int scissor = 3;// waarde van scissor         scanner keyboard = new scanner (system.in);// naam scanner input         random rand = new random();         boolean playing = true;         boolean ans = false;         string ans0 = "";           while(playing == true){             int ai = rand.nextint((3 - 1) +1) + 1;// maakt int ai een random getal nog niet af moet een range in komen                system.out.println("choose 1 rock, 2 paper , 3 scissor"); // string met keuze voor user             int userchoice = keyboard.nextint();// waarde van userchoice                  if (ai == rock && userchoice == paper || ai == paper && userchoice == scissor || ai == scissor && userchoice == rock){                 system.out.println("you win");                 if (ai == rock)                 system.out.println("the computer did rock");                 if (ai == paper)                 system.out.println("the computer did paper");                 if (ai == scissor)                 system.out.println("the computer did scissor");                 }                  else if (ai == userchoice) {                 system.out.println("draw");                 if (ai == rock);                 system.out.println("the computer did rock");                 if (ai == paper)                 system.out.println("the computer did paper");                 if (ai == scissor)                 system.out.println("the computer did scissors");                 }                  else if (ai == rock && userchoice == scissor || ai == paper && userchoice == rock || ai == scissor && userchoice == paper){                 system.out.println("you lose");                 if (ai == rock)                 system.out.println("the computer did rock");                 if (ai == paper)                 system.out.println("the computer did paper");                 if (ai == scissor)                 system.out.println("the computer did scissors");                 }                                 while (ans == false){             system.out.println("do want try again?");             ans0 = keyboard.nextline();             ans0 = ans0.tolowercase();             switch(ans0){                 case "yes":                     ans = true;                     playing = true;                     break;                 case "no":                     ans = true;                     playing = false;                     break;                 }                         }             } } 

because there not { } block after while condition while block 1 line after it. repeating sysout endlessly.

this more attempting do.

****edit*** other answer describes there second issue switch statments. try manner exit.

        while (ans == false) {  // start of while block           system.out.println("do want try again?");           ans0 = keyboard.nextline();           ans0 = ans0.tolowercase();           switch(ans0){             case "no":               playing = false;             case "yes":               ans = true;           }         }    // end of while block 

the above loop until answer either "yes" or "no", @ point sets ans = true exit while (ans == false) loop.

if answer "no" see playing = false exit above playing == true loop, otherwise loop game again.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -