java - RPS game making it say invalid error -


i made rock paper scissors game cnt figure out how make show invalid error when user enters other r,p,s. helpful if once tell me how make this. im relatively new coder. in advance help

import java.util.random; import java.util.scanner;  public class rps { public static void main(string[] args)  { string userplay; //user's play -- "r", "p", or "s"  string computerplay = ""; //computer's play -- "r", "p", or "s"  int computerint;  string response;    scanner scan = new scanner(system.in);  random generator = new random();   system.out.println("lets play rock, paper, scissors!\n" +                     "choose move.\n" + "rock = r, paper" +                     "= p, , scissors = s.");  system.out.println();  //generate computer's play (0,1,2)  computerint = generator.nextint(3)+1;   //translate computer's randomly generated play  //string using if //statements   if (computerint == 1)     computerplay = "r";  else if (computerint == 2)     computerplay = "p";  else if (computerint == 3)     computerplay = "s";    //get player's play input-- note r system.out.println("enter play: ");  userplay = scan.next();  //make player's play uppercase userplay = userplay.touppercase();   //print computer's play  system.out.println("your opponents play is: " + computerplay);    //see won.   if (userplay.equals(computerplay))     system.out.println("it's tie!");  else if (userplay.equals("r"))     if (computerplay.equals("s"))        system.out.println("rock breaks scissors. win!!"); else if (computerplay.equals("p"))          system.out.println("paper covers rock. lose!!");  else if (userplay.equals("p"))     if (computerplay.equals("s"))     system.out.println("scissor cuts paper. lose!!");  else if (computerplay.equals("r"))          system.out.println("paper covers rock. win!!");  else if (userplay.equals("s"))       if (computerplay.equals("p"))       system.out.println("scissor cuts paper. win!!");  else if (computerplay.equals("r"))          system.out.println("rock breaks scissors. lose!!");  else       system.out.println("invalid user input.");  } } 

i think best way loop. ask enter player's move, if unacceptable ask again. continue until user enters valid string. in code this

//get player's play input boolean moveok = false; while (moveok == false) {     system.out.println("enter play: ");     userplay = scan.next();     //make player's play uppercase     userplay = userplay.touppercase();     // check input ok     if ("r".equals(userplay) ||         "p".equals(userplay) ||         "s".equals(userplay)) {          moveok = true;     } else {         system.out.println("bad input, try again!");     } } 

if want show error , stop program, instead of creating loop can use simple if similar condition , when input incorrect print error message , use return.

also make code easier understand if divide conditions in parts, example user input. see example below.

//see won.  string result = ""; if (userplay.equals(computerplay)) {     result = "it's tie!"; } if (userplay.equals("r")) {     if (computerplay.equals("s")) {         result = "rock breaks scissors. win!!";     }      if (computerplay.equals("p")) {         result = "paper covers rock. lose!!";     } } if (userplay.equals("p")) {     if (computerplay.equals("s")) {         result = "scissor cuts paper. lose!!";     }      if (computerplay.equals("r")) {         result = "paper covers rock. win!!";     } } if (userplay.equals("s")) {     if (computerplay.equals("p")) {         result = "scissor cuts paper. win!!";     }      if (computerplay.equals("r")) {         result = "rock breaks scissors. lose!!";     } } system.out.println(result); 

also use switch statement.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -