java - what's the wrong that i fell in with stone.scissors&paper game? -


my code results in exception .. can me spot problem?

import java.util.scanner;  import java.util.random;  class apples {      public static void main(string[] args){     scanner sps = new scanner(system.in);     system.out.println("choose:\tstone,paper or scissors");     sps.hasnext();      random rand = new random(2);      int scissors = rand.nextint(0);     int stone = rand.nextint(1);     int paper = rand.nextint(2);      system.out.println((sps.hasnext("scissors")||sps.hasnext("stone")||sps.hasnext("paper"))? null:"go play away here");      if(rand.nextint() == 0 && sps.hasnext("scissors")){         system.out.println("scissors");         system.out.println("you tie");      }else if(rand.nextint() == 0 && sps.hasnext("stone")){          system.out.println("scissors");         system.out.println("you lose");      }else if(rand.nextint() == 0 && sps.hasnext("paper")){          system.out.println("scissors");         system.out.println("you won");       }else if(rand.nextint() == 1 && sps.hasnext("stone")){         system.out.println("stone");         system.out.println("you tie");      }else if(rand.nextint() == 1 && sps.hasnext("scissors")){          system.out.println("stone");         system.out.println("you won");      }else if(rand.nextint() == 1 && sps.hasnext("paper")){          system.out.println("stone");         system.out.println("you lose");      }else if(rand.nextint() == 2 && sps.hasnext("paper")){         system.out.println("paper");         system.out.println("you tie");      }else if(rand.nextint() == 2 && sps.hasnext("stone")){          system.out.println("paper");         system.out.println("you won");      }else if(rand.nextint() == 2 && sps.hasnext("scissors"))          system.out.println("paper");         system.out.println("you lose");       } } 

and result

choose: stone,paper or scissors stone exception in thread "main" java.lang.illegalargumentexception: n must positive     @ java.util.random.nextint(unknown source)     @ stone.paper.scissors.apples.main(apples.java:20) 

well, problem here:

int scissors = rand.nextint(0); 

because, said in exception, 0 not positive.

what wanted is:

int scissors = 0; int stone = 1; int paper = 2; 

and use variables after when comparing:

if (!sps.hasnext("(scissors|stone|paper)")) {     system.out.println("go play away here"); } else {      int randomchoice = rand.nextint(3);     string userchoice = sps.next();      if(randomchoice == scissors && userchoice.equals("scissors")){         system.out.println("scissors");         system.out.println("you tie");     }else if(randomchoice == scissors && userchoice.equals("stone")){         system.out.println("scissors");         system.out.println("you lose");     }else if(randomchoice == scissors && userchoice.equals("paper")){         system.out.println("scissors");         system.out.println("you won");     }else if(randomchoice == stone && userchoice.equals("stone")){         system.out.println("stone");         system.out.println("you tie");     }else if(randomchoice == stone && userchoice.equals("scissors")){         system.out.println("stone");         system.out.println("you won");     }else if(randomchoice == stone && userchoice.equals("paper")){         system.out.println("stone");         system.out.println("you lose");     }else if(randomchoice == paper && userchoice.equals("paper")){         system.out.println("paper");         system.out.println("you tie");     }else if(randomchoice == paper && userchoice.equals("stone")){         system.out.println("paper");         system.out.println("you won");     }else if(randomchoice == paper && userchoice.equals("scissors")) {         system.out.println("paper");         system.out.println("you lose");     } } 

note: corrected:

  • your incorrect use of rand
  • your incorrect use of scanner
  • your missing brackets last else if.

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -