我是Java新手,正在构建Rock,Paper,Scissors游戏。我试图找出为什么我的while语句在第一次尝试时无法识别正确的输入。感谢您提前提供的任何帮助。
`
import java.util.Scanner;
import java.util.Random;
public class RPC
{
public static void main (String[] args)
{
//Utilities
Scanner in = new Scanner (System.in);
Random r = new Random ();
//variables
int userwins =0;
int computerwins =0;
int numberofgames = 0;
int randomnumber = 0;
String userpick = "";
String computerpick = "";
// Welcome statement
System.out.println("Welcome to Rock, Paper, Scissors!");
//Get the number of games from the user
System.out.println("Please enter the number of rounds you would like to play:");
numberofgames = in.nextInt();
//Flush the buffer
in.nextLine();
// Game play for the number of times the user specified.
for (int i=1; i <= numberofgames; i++)
{
System.out.println("Rock, Paper, or Scissors?:");
userpick = in.nextLine();
while (!userpick.equalsIgnoreCase("rock") && !userpick.equalsIgnoreCase("paper") && !userpick.equalsIgnoreCase("scissors"));
{
System.out.println("Sorry," +userpick+ " is not a valid entry.Please enter Rock, Paper, or Scissors.");
userpick = in.nextLine();
}
//Make a random choice
randomnumber = r.nextInt(3)+1;
if (randomnumber == 1)
{
computerpick = ("rock");
}
if (randomnumber == 2)
{
computerpick = ("paper");
}
if (randomnumber == 3)
{
computerpick = ("scissors");
}
//Decide who wins the round
if (userpick.equalsIgnoreCase("rock")) {
if (computerpick.equalsIgnoreCase("scissors"))
System.out.println("Rock beats scissors. You win!");
else if (computerpick.equalsIgnoreCase("paper"))
System.out.println("Paper beats rock. The computer wins!");
else
System.out.println("Computer chooses rock. It's a tie.");
}
else if (userpick.equalsIgnoreCase("paper")) {
if (computerpick.equalsIgnoreCase("scissors"))
System.out.println("Scissors beats paper. The computer wins!");
else if (computerpick.equalsIgnoreCase("rock"))
System.out.println("Paper beats rock. You win!");
else
System.out.println("Computer chooses paper. It's a tie.");
}
else if (userpick.equalsIgnoreCase("scissors")) {
if (computerpick.equalsIgnoreCase("rock"))
System.out.println("Rock beats scissors. The computer wins!");
else if (computerpick.equalsIgnoreCase("paper"))
System.out.println("Scissors beats paper. You win!");
else
System.out.println("Computer chooses scissors. It's a tie.");
}
}
} }`
答案 0 :(得分:2)
删除终止while
语句
while (...);
^
答案 1 :(得分:0)
在for循环后删除分号。 while / for / if之后的分号表示它是一个空语句,这意味着带有括号的下一个代码块就像一个块一样,无论条件是否成立,它都会被评估。
答案 2 :(得分:0)
分号通常在java中结束一行。为了使while循环运行,您需要删除半列。