我正在用 Java 编写一个代码,这是一个石头剪刀布游戏。特别是,我试图将随机输出(即用户输了、赢了或平局)计入分数。
例如,用户输入石头、布或剪刀。如果他们选择摇滚,他们将得到一个随机输出,决定他们是否获胜、失败或平局。如果他们得到输出,我们可以说:“你选择了石头。我选择了纸。你输了!”我想将该分数结果添加为“1 负”。在我的 while 循环中。但是,我不知道如何让程序像这样计算随机输出。
我的代码如下,我在其中添加了注释,以尝试更好地解释我想要做什么。
import java.util.Random;
import java.util.Scanner;
public class rockpaperscissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
System.out.println("Please choose (r)ock, (p)aper or (s)cissors:");
String answer = input.nextLine();
int win = 0;
int lose = 0;
int tie = 0;
if(answer.equals("r")) {
String[] str = { "You chose rock. I chose paper. You lose!", /* lose++; */ "You chose rock. I chose scissors. You win!", /* win++; */ "You chose rock. I chose rock. We tie!" /* tie++; */ };
Random r = new Random();
System.out.println(str[r.nextInt(3)]);
}
if(answer.equals("p")) {
String[] str = { "You chose paper. I chose scissors. You lose!", /* lose++; */ "You chose paper. I chose rock. You win!", /* win++; */ "You chose paper. I chose paper. We tie!" /* tie++; */ };
Random r = new Random();
System.out.println(str[r.nextInt(3)]);
}
if(answer.equals("s")) {
String[] str = { "You chose scissors. I chose rock. You lose!", /* lose++; */ "You chose scissors. I chose paper. You win!", /* win++; */ "You chose scissors. I chose scissors. We tie!" /* tie++; */ };
Random r = new Random();
System.out.println(str[r.nextInt(3)]);
}
System.out.println("Score: " + win + " win(s), " + lose + " loss(es), " + tie + " tie(s)");
System.out.println("Play again (y/n)?");
String c = input.nextLine();
if(c.equalsIgnoreCase("n")){
break;
}
}
}
}
我尝试过使用 switch 语句,但没有成功。我也一直在尝试使用 if 语句,但我的代码从来没有工作过。请注意,我对 java 还很陌生,所以有些功能我很不熟悉。
您需要在循环外部使用变量,因为每次循环时循环内部的变量都会重置
示例代码
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int win = 0;
int lose = 0;
int tie = 0;
while(true){
System.out.println("Please choose (r)ock, (p)aper or (s)cissors:");
String answer = input.nextLine();
if(answer.equals("r")) {
String[] str = { "You chose rock. I chose paper. You lose!", /* lose++; */ "You chose rock. I chose scissors. You win!", /* win++; */ "You chose rock. I chose rock. We tie!" /* tie++; */ };
Random r = new Random();
int i = r.nextInt(3);
if( i == 0 ) ++lose;
if( i == 1 ) ++win;
if( i == 2 ) ++tie;
System.out.println(str[i]);
}
if(answer.equals("p")) {
String[] str = { "You chose paper. I chose scissors. You lose!", /* lose++; */ "You chose paper. I chose rock. You win!", /* win++; */ "You chose paper. I chose paper. We tie!" /* tie++; */ };
Random r = new Random();
int i = r.nextInt(3);
if( i == 0 ) ++lose;
if( i == 1 ) ++win;
if( i == 2 ) ++tie;
System.out.println(str[i]);
}
if(answer.equals("s")) {
String[] str = { "You chose scissors. I chose rock. You lose!", /* lose++; */ "You chose scissors. I chose paper. You win!", /* win++; */ "You chose scissors. I chose scissors. We tie!" /* tie++; */ };
Random r = new Random();
int i = r.nextInt(3);
if( i == 0 ) ++lose;
if( i == 1 ) ++win;
if( i == 2 ) ++tie;
System.out.println(str[i]);
}
System.out.println("Score: " + win + " win(s), " + lose + " loss(es), " + tie + " tie(s)");
System.out.println("Play again (y/n)?");
String c = input.nextLine();
if(c.equalsIgnoreCase("n")){
break;
}
}
}