我编写了一个工作正常的RPS程序。我唯一的问题是它必须永远持续下去,直到用户的输入是"停止"。我尝试使用while循环执行此操作但是我得到一个奇怪的输出,一直要求用户的输入。我知道我的一个错误是我需要在while循环中提示输入,但是我将如何开始使用while循环。
import java.util.*;
public class Navairps
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
play(input,rand);
}
//outputs the description and rules of the game
public static void description()
{
System.out.println("Using this app you can play Rock-Paper-Scissors game against the computer. When played between");
System.out.println("two people, each person picks one of the three options at the same time, and the winner");
System.out.println("Is determined. The program should randomly choose one of the three options, then prompt for the");
System.out.println("user's selection. At that point, the program reveals both choices and print a");
System.out.println("Statement indicating if the user won, the computer won, or if it was a tie. Continue playing until the");
System.out.println("user choose to stop. Then print the total number of the games played, total wins, total losses, and");
System.out.println("total ties.");
System.out.println("Ready, Set, Go\n");
System.out.println("your choices");
System.out.println("\tRock");
System.out.println("\tPaper");
System.out.println("\tScissors");
System.out.println("\tstop");
}
//generates a random number (0-2) for the computer selection
public static String computerRandom(Random rand)
{
int computer = rand.nextInt(3);
String computerInput=("");
switch (computer)
{
case 0: computerInput = "rock";
break;
case 1: computerInput = "paper";
break;
case 2: computerInput = "scissors";
break;
}
return computerInput;
}
//gets the users choice via Scanner class and returns it, must check if input is valid
public static String userInput(Scanner input)
{
System.out.print("Enter your choice: ");
String choice = input.nextLine();
choice = choice.toUpperCase();
return choice;
}
//initiates the game, allows user to keep playing, keeps track of score
public static void play(Scanner input, Random rand)
{
description();
int wins=0;
int draws=0;
int losses=0;
String choice = userInput(input);
while (!choice.equals("STOP"));
{
String computerChoice = computerRandom(rand);
System.out.println("Computer selected: " + computerChoice);
computerChoice = computerChoice.toUpperCase();
System.out.println("You selected: " + choice);
if (computerChoice.equals(choice))
{
System.out.println("There is a tie");
draws++;
//add 1 to ties
}
if (computerChoice.equals("ROCK") && choice.equals("SCISSORS"))
{
System.out.println("Oh No, you lost");
losses++;
//add 1 to losses
}
if (computerChoice.equals("SCISSORS") && choice.equals("ROCK"))
{
System.out.println("Hurray! You won");
wins++;
//add 1 to wins
}
if (computerChoice.equals("PAPER") && choice.equals("ROCK"))
{
System.out.println("Oh No, you lost");
losses++;
//add 1 to losses
}
if (computerChoice.equals("ROCK") && choice.equals("PAPER"))
{
System.out.println("Hurry! You won");
wins++;
//add 1 to wins
}
if (computerChoice.equals("PAPER") && choice.equals("SCISSORS"))
{
System.out.println("Hurry! You won");
wins++;
//add 1 to wins
}
if (computerChoice.equals("SCISSORS") && choice.equals("PAPER"))
{
System.out.println("Oh No, you lost");
losses++;
//add 1 to losses
}
}
/*if (choice.equals("stop"));
{
System.out.print("come back soon");
}*/
}
}
答案 0 :(得分:1)
这是do{} while()
循环的教科书示例。而不是使用while循环,更改您的代码如下:
public static void play(Scanner input, Random rand)
{
...
do {
choice = userInput(input);
String computerChoice = computerRandom(rand);
...
} while (!choice.equalsIgnoreCase("STOP"));
}
这将解决您的问题。
但是,当您发出STOP命令时,您可能不希望游戏再次计算随机值,因此您可能需要额外的 if -condition来阻止计算机播放进一步:
if (!choice.equalsIgnoreCase("STOP")){
String computerChoice = computerRandom(rand);
System.out.println("Computer selected: " + computerChoice);
...
}
请注意,除了使用 if -statement之外,您还可以使用break
来摆脱循环:if (choice.equalsIgnoreCase("STOP")) break;
答案 1 :(得分:0)
在应用程序/程序的加载时启动while循环,然后使用break;
结束if条件下的while循环
答案 2 :(得分:0)
这里至少有两个问题
停止命令为STOP
而不是小写stop
choice = userInput(input)
仅在循环外设置一次
正如之前在一些评论中所述,do {} while()
是可行的方法。
请注意,您可以使用equalsIgnoreCase()
代替equals()
,因此您无需将所有内容转换为大写。