我的代码导致异常..你能帮我发现我的问题吗?
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 from 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");
}
}
和结果
Choose: stone,paper or scissors
stone
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at stone.paper.scissors.apples.main(apples.java:20)
答案 0 :(得分:2)
嗯,你的问题在这里:
int scissors = rand.nextInt(0);
因为,如异常中所述,0不是正数。
您想要做的是:
int scissors = 0;
int stone = 1;
int paper = 2;
并在比较后使用变量:
if (!SPS.hasNext("(scissors|stone|paper)")) {
System.out.println("go play away from 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");
}
}
注意:我也更正了:
rand
Scanner
else if
的缺失括号。