Java 爪哇石剪刀
查看了另一个线程,但我无法理解为什么我的程序在输入我的选择(即Rock)后会给出2个答案,或者根本没有答案。我知道这和之前的帖子很相似,但我不明白为什么回复会有所不同。任何帮助都将不胜感激Java 爪哇石剪刀,java,Java,查看了另一个线程,但我无法理解为什么我的程序在输入我的选择(即Rock)后会给出2个答案,或者根本没有答案。我知道这和之前的帖子很相似,但我不明白为什么回复会有所不同。任何帮助都将不胜感激 import java.util.Random; import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { System.out.println("Do you w
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
System.out.println("Do you want to play Rock Paper Sissors?");
System.out.println("Yes or No?");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if (input.equals("Yes")){
System.out.println("OK pick your weapon!");
String weapon = scan.nextLine();
Random rand = new Random();
rand.nextInt(3);
if (rand.nextInt(3) == 0){
//*0=Rock
if (weapon.equals("Rock")) {
System.out.println("I choose Rock, so did you... tie.");
}
else if (weapon.equals("Paper")){
System.out.println("I choose Rock and you choose Paper...You Win :( !");
}
else if (weapon.equals("Scissors")){
System.out.println("I choose Rock and you choose Paper...You Lose :) !");
}
}
if (rand.nextInt(3) == 1){
//*1=Paper
if (weapon.equals("Rock")) {
System.out.println("I choose Paper and you choose Rock...You Lose :) !");
}
else if (weapon.equals("Paper")){
System.out.println("I choose Paper, so did you...tie.");
}
else if (weapon.equals("Scissors")){
System.out.println("I choose Paper and you choose Scissors...You Win :( !");
}
}
if (rand.nextInt(3) == 2){
//*2=Scissors
if (weapon.equals("Rock")) {
System.out.println("I choose Scissors and you choose Rock...You Win :( !");
}
else if (weapon.equals("Paper")){
System.out.println("I choose Scissors and you choose Paper...You Lose:) !");
}
else if (weapon.equals("Scissors")){
System.out.println("I choose Scissors, so did you...tie.");
}
}
}
else {
System.out.println("OK have a nice day.");
}
}
}
感谢您的帮助。您有三条if语句,每条语句都生成自己的随机值,因此可以执行所有三条if语句。您应该保存rand.nextInt(3)的结果一次,并在三个连续的if语句中使用该结果。我将把一行中的三个if语句也更改为一系列if/elseif语句 你打电话的地方似乎几乎找到了正确的解决方案
rand.nextInt(3);
在初始if语句之前,只需将结果保存到一个变量。您知道什么是调试器了吗?试着拿些笔和纸,画出程序的执行情况。