必须反复重新提交用户选择



嘿,伙计们在多部分问题上遇到了一些麻烦。我正试图完成一个石头剪刀游戏,当我试图使用playRound方法测试代码时,我必须一遍又一遍地输入我的选择,然后它就会打印出平局,你是赢家,电脑是赢家,有人能告诉我哪里出了问题吗?

import java.util.Scanner;
import java.util.Random;
/**
* A class that will play a game of rock paper scissors.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RockPaperScissors
{
private Scanner reader;
private int yourScore;
private int computerScore;
private Random ran = new Random();
public RockPaperScissors()
{
reader = new Scanner(System.in);
yourScore = 0;
computerScore=0;
Random ran = new Random(); 
}
public void printPrompt()
{
System.out.println("Enter your choice, paper, rock or scissors >"); 
String userChoice = userChoice();
System.out.println();
System.out.println();
System.out.println("Enter your choice, paper, rock or scissors  >"+ userChoice);
}
public final String userChoice()
{
String userChoice= reader.next();
return userChoice;
}
public String computerChoice()
{
String compMove = ("");
int cpuChoice = ran.nextInt(3);
switch(cpuChoice)
{
case 0:
{
compMove = ("rock");
break;
}
case 1:
{
compMove = ("paper");
break;
}
case 2:
{
compMove = ("scissors");
break;
}
}
return (compMove);
}
public String findWinner(String yourChoice, String computerChoice) 
{
yourChoice = userChoice();
computerChoice = computerChoice();
String Winner= null;
if (yourChoice.equals(computerChoice))
{
Winner = ("draw");
}
if (yourChoice.equals("rock")) 
{
if (computerChoice.equals("paper")) 
{
computerScore++;
Winner = ("computer");
}
else if (computerChoice == "scissors")
{
yourScore++;
Winner = ("you");
}
}
if (yourChoice.equals("paper"))
{
if (computerChoice.equals("scissors"))
{
computerScore++;
Winner = ("computer");
}
else if (computerChoice.equals("rock")) 
{
yourScore++;
Winner = ("you");
}
}
if (yourChoice.equals("scissors"))
{
if (computerChoice.equals("rock"))
{
computerScore ++;
Winner = ("computer");
}
else if (computerChoice.equals("paper"))
{
yourScore++;
Winner = ("you");
}
}
if (!yourChoice.equals("rock||paper||scissors")) 
{
computerScore++;
Winner = ("computer");
}
return Winner;
}
public void playRound()
{
printPrompt();
String computerChoice=computerChoice();
String userChoice=userChoice();
System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);
String findWinner = findWinner(computerChoice,userChoice);

{
if (findWinner.equals("draw"));
System.out.println("This game is a draw");
if (findWinner.equals("you"));
System.out.println("You are the winner");
if (findWinner.equals("computer"));
System.out.println("The computer is the winner");
}
System.out.println("You have " + yourScore + "and the computer has "+ computerScore);
}
}

if语句末尾的分号表示if语句已完成。紧接在if语句之后的行是独立的;归属;到if语句。。。这就是为什么他们总是跑。

更改:

if (findWinner.equals("draw"));
System.out.println("This game is a draw");

收件人:

if (findWinner.equals("draw"))
System.out.println("This game is a draw");

甚至更好:

if (findWinner.equals("draw")) {
System.out.println("This game is a draw");
}

关于如何保存我的初始选择的任何建议,这样我就不会必须不断地输入?

findWinner()中,您将再次调用userChoice()computerChoirce()。此时,这些值已经在传入的参数中,因此您不需要这些值。注释掉或删除它们:

public String findWinner(String yourChoice, String computerChoice) 
{
// You don't need these two lines:
//yourChoice = userChoice();
//computerChoice = computerChoice(); 
// ... other existing code ...
}

然后,在playRound()中,您同时调用printPrompt()userChoice(),它们都有自己的输入机制:

printPrompt();
String computerChoice=computerChoice();
String userChoice=userChoice();
System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);

我会去掉printPrompt(),只做:

// printPrompt();
String computerChoice=computerChoice();
System.out.print("Enter your choice, paper, rock or scissors >"); 
String userChoice=userChoice();
System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);

findWinner()中存在一些逻辑故障。这并不像你想象的那样:

if (!yourChoice.equals("rock||paper||scissors")) 
{
computerScore++;
Winner = ("computer");
}

我认为如果用户没有输入";岩石"纸";,或";剪刀";?如果是,请将if语句更改为else if语句,并添加最后一个else块:

public String findWinner(String yourChoice, String computerChoice) 
{
String Winner = "";
if (yourChoice.equals(computerChoice))
{
Winner = "draw";
}
else if (yourChoice.equals("rock")) 
{
if (computerChoice.equals("paper")) 
{
computerScore++;
Winner = "computer";
}
else if (computerChoice.equals("scissors"))
{
yourScore++;
Winner = "you";
}
}
else if (yourChoice.equals("paper"))
{
if (computerChoice.equals("scissors"))
{
computerScore++;
Winner = "computer";
}
else if (computerChoice.equals("rock")) 
{
yourScore++;
Winner = "you";
}
}
else if (yourChoice.equals("scissors"))
{
if (computerChoice.equals("rock"))
{
computerScore ++;
Winner = "computer";
}
else if (computerChoice.equals("paper"))
{
yourScore++;
Winner = "you";
}
}
else // user did not type "rock", "paper", or "scissors"!
{
computerScore++;
Winner = ("computer");
}
return Winner;
}

最后,MAJOR错误,您在调用findWinner()时交换了参数的顺序。

更改:

String findWinner = findWinner(computerChoice, userChoice);

收件人:

String findWinner = findWinner(userChoice, computerChoice);

希望能有所帮助!

最新更新