我试图理解为什么我的代码只能工作一次然后有时根本不工作。有时它会接受我输入的字符串,然后有时它根本不会工作。请指出我正确的方向。
static int yourChoice, computerChoice;
static String userChoice;
static String play = " ";
static int tieGames;
static int wonGames;
static int lossGames;
static int totalGames;
public static void main (String args[]){
//Variable declaration
//your choice versus the computer choice will be evaluated
//the input
do{
PrintExplanation();
String userChoice;
userChoice =JOptionPane.showInputDialog(null, "Enter one of the following as your choice: \n Rock: (valid choices include 0, r, rock) \n Paper: (valid choices include 1, p, paper) \n Scissors: (valid choices include 2, s, scissors)");
yourChoice = Integer.parseInt(userChoice);
ConvertStringtoNum(userChoice);
yourChoice= ConvertStringtoNum(userChoice);
while(yourChoice < 0 || yourChoice > 2){
JOptionPane.showMessageDialog(null,"Please only type in 0, 1, 2, r, p, s, rock, paper, or sissors for your choice!");
if(yourChoice == 0){
break;
}
else if(yourChoice == 1){
break;
}
else if(yourChoice == 2){
break;
}
userChoice =JOptionPane.showInputDialog(null, "Enter one of the following as your choice: \n Rock: (valid choices include 0, r, rock) \n Paper: (valid choices include 1, p, paper) \n Sissors: (valid choices include 2, s, sissors)");
}
computerChoice = GetComputerChoice();
DetermineWinner(yourChoice,computerChoice);
totalGames++;
play = JOptionPane.showInputDialog("Do you want to play again? yes or no?!");
if(play.equalsIgnoreCase("no")) {
break;
}
}while(play.equalsIgnoreCase("yes"));
JOptionPane.showMessageDialog(null,"Total Games: "+totalGames+ "\n Total Wins: "+wonGames+ "\n Total Losses: " +lossGames+ "\nTotal Tied Games: " +tieGames);
}
public static void PrintExplanation(){
//Welcome screen and show the basic rule to user
JOptionPane.showMessageDialog(null,"Welcome to the Rock, Paper, Sissors Game! \n Here are the rules:\nScissor cuts paper, paper covers rock, and rock breaks scissors. \n 0:Rock\n1: Paper\n2:Scissors");
}
public static String GetUserChoice(String userChoice){
//Acquire a users choice
userChoice =JOptionPane.showInputDialog(null, "Enter one of the following as your choice: \n Rock: (valid choices include 0, r, rock) \n Paper: (valid choices include 1, p, paper) \n Scissors: (valid choices include 2, s, scissors)");
yourChoice = Integer.parseInt(userChoice);
return userChoice ;
}
public static int ConvertStringtoNum(String userChoice){
if (userChoice.equals("rock") || userChoice.equals("0")){
yourChoice = 0;
}
else if(userChoice.equals("r") || userChoice.equals("0")){
yourChoice = 0;
}
else if(userChoice.equals("paper") || userChoice.equals("1")){
yourChoice = 1;
}
else if(userChoice.equals("p") || userChoice.equals("1")){
yourChoice = 1;
}
else if(userChoice.equals("scissors") || userChoice.equals("2")){
yourChoice = 2;
}
else if(userChoice.equals("s") || userChoice.equals("2")){
yourChoice = 0;
}
return yourChoice = 1;
}
public static int GetComputerChoice(){
computerChoice = (int)(Math.random() * 3);
return computerChoice = (int)(Math.random() * 3);
}
public static int DetermineWinner(int yourChoice, int computerChoice){
//determines the winner
if(yourChoice == computerChoice){
JOptionPane.showMessageDialog(null,"It's a tie!");
tieGames++;
}
else if (yourChoice==0 && computerChoice==2 || userChoice.equals("r") && computerChoice==2 || userChoice.equals("rock") && computerChoice==2){
JOptionPane.showMessageDialog(null,"You chose:Rock \n Computer chose:Scissors \n Rock beats Scissors You WIN!");
wonGames++;
}
else if (yourChoice==1 && computerChoice==0 || userChoice.equals("p") && computerChoice==0 || userChoice.equals("paper") && computerChoice==0){
JOptionPane.showMessageDialog(null,"You chose:Paper \n Computer chose:Rock \n Paper beats Rock You WIN!");
wonGames++;
}
else if (yourChoice==2 && computerChoice==1 || userChoice=="s" && computerChoice==1 || userChoice=="sissors" && computerChoice==1){
JOptionPane.showMessageDialog(null,"You chose:Sissors \n Computer chose:Rock \n Scissors beats Paper You WIN!");
wonGames++;
}
else if(yourChoice==1 && computerChoice==2 || userChoice=="p" && computerChoice==2 || userChoice=="paper" && computerChoice==2){
JOptionPane.showMessageDialog(null,"You chose:Paper \n Computer chose:Scissors \n Paper loses against Scissors YOU LOSE! :-(");
lossGames++;
}
else if(yourChoice==2 && computerChoice==0 || userChoice=="s" && computerChoice==0 || userChoice=="sissors" && computerChoice==0){
JOptionPane.showMessageDialog(null,"You chose:Scissors \n Computer chose:rock \n Scissors lose against Rock YOU LOSE! :-(");
lossGames++;
}
else if (yourChoice==0 && computerChoice==1 || userChoice=="r" && computerChoice==1 || userChoice=="rock" && computerChoice==1){
JOptionPane.showMessageDialog(null,"You chose:Rock \n Computer chose:Paper \n Rock loses against Paper YOU LOSE! :-(");
lossGames++;
}
return computerChoice;
}
}
答案 0 :(得分:0)
@juned是对的
这对于整数来说是可以的
yourChoice==0 && computerChoice==2
这是比较字符串的方法:
userChoice.equals("r")
将所有字符串比较转换为equals()
你发了很多代码,但这是一个例子:
else if (yourChoice==2 && computerChoice==1 || userChoice=="s" && computerChoice==1 || userChoice=="sissors" && computerChoice==1){
答案 1 :(得分:0)
所以从你到目前为止发布的代码中我可以看到三个问题:
userChoice =JOptionPane.showInputDialog(null, "Enter one of the following as your choice: \n Rock: (valid choices include 0, r, rock) \n Paper: (valid choices include 1, p, paper) \n Scissors: (valid choices include 2, s, scissors)");
yourChoice = Integer.parseInt(userChoice);
ConvertStringtoNum(userChoice);
yourChoice= ConvertStringtoNum(userChoice);
您尝试解析userChoice
,但仍然不知道它是否是Integer
。如果玩家输入的内容不是0, 1
或2
,则会引发NumberFormatException
。
因为您已经拥有方法ConvertStringtoNum
,并且您将yourChoice
覆盖到其结果,无论如何您可以完全发出行yourChoice= ConvertStringtoNum(userChoice);
而不会冒险抛出NumberFormatException
此外,您无需再拨打ConvertStringtoNum
两次。
更好:
userChoice =JOptionPane.showInputDialog(null, "Enter one of the following as your choice: \n Rock: (valid choices include 0, r, rock) \n Paper: (valid choices include 1, p, paper) \n Scissors: (valid choices include 2, s, scissors)");
yourChoice= ConvertStringtoNum(userChoice);
然后第二个问题是方法ConvertStringtoNum
本身。
你写了很多if {} else if {}
来获得正确的yourChoice
值,但最后你写了return yourChoice = 1
所以无论用户的输入是什么,你都会返回{{1}在这个方法中。您只需将最后一行更改为1
,一切都应该有效。
第三个问题是方法return yourChoice;
。
您使用DetermineWinner
。要比较字符串,你应该使用userChoice=="sissors"
(就像你大部分时间一样。其次,此时你甚至不需要再看.equals
,因为你已经将它的值解析为{{ 1}}。因此,您也可以简单地从userChoice
中删除与yourChoice
的每次比较。
userChoice
和DetermineWinner
的更简洁方式:
DetermineWinner