gpt4 book ai didi

java - 剪刀石头布。 java 。方法

转载 作者:行者123 更新时间:2023-12-01 23:26:00 25 4
gpt4 key购买 nike

我假设这是一个逻辑错误。我无法将结果相加,也无法得到正确的结果。每次我输入石头、剪刀、布时,它都会从那里决定我是赢了、输了还是平局。我的代码有什么问题吗?

public class RockPaperScissors {

public static void displayGreeting()
{
String intro = "This program is a game. A game of Rock, Paper, Scissors\n"+
"It is you against the computer. Rock beats scissors, Paper\n"+
" beats rock, and scissors beats paper. Good luck and may the\n"+
"odds be ever in your favor.";
JOptionPane.showMessageDialog(null, intro, "Rock Paper Scissors",1);
}

public static String generateComputersChoice()
{
Random randomGenrator = new Random();
int randomNumber = randomGenrator.nextInt(3);

String weapon = "nothing";
switch(randomNumber){
case 0: weapon = "rock";
break;
case 1: weapon = "paper";
break;
case 2: weapon = "scissors";
break;
}
return weapon;
}

public static String enterPlayersChoice(){

String prompt = "You have a choice of picking rock, paper, or scissors.\n"+
"Choose wisely.";

String input = "";

input = JOptionPane.showInputDialog(null,prompt,"Choose your weapon",1);
String inputLower = input.toLowerCase();
return inputLower;

}

public static void main(String[] args)
{
displayGreeting();
// generateComputersChoice();
//enterPlayersChoice();
// JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(5));

String player = enterPlayersChoice();
String comp = generateComputersChoice();

int ties = 0;
int playerWins = 0;
int compWins = 0;

for(int i = 0; i < 3; i ++){



//enterPlayersChoice(); //method

//generateComputersChoice(); //method
//JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(1));

//System.out.println(player+ " " + comp);
//JOptionPane.showMessageDialog(null,player+ " " +comp);

if(player.equals(comp)){
JOptionPane.showMessageDialog(null, "It's a tie!");
ties ++;

}
else if(player.equals("rock")){
if(comp.equals("scissors")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;

}
}else if(comp.equals("rock")){
if(player.equals("scissors")){
JOptionPane.showMessageDialog(null, "You lose!");
compWins ++;

}
}else if(player.equals("scissors")){
if(comp.equals("paper")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;

}
}else if(comp.equals("scissors")){
if(player.equals("paper")){
JOptionPane.showMessageDialog(null, "You lose");
compWins ++;

}
}else if(player.equals("paper")){
if(comp.equals("rock")){
JOptionPane.showMessageDialog(null, "You Win!");
playerWins ++;
}
}else if(comp.equals("paper")){
if(player.equals("rock")){
JOptionPane.showMessageDialog(null, "You lose!");
compWins ++;
}
}else{
JOptionPane.showMessageDialog(null, "Invalid user input");
i--;
}


}
//Results
JOptionPane.showMessageDialog(null,"Here are the results\n\n"+
"\nTies: " +ties+
"\nComputer Wins: " +compWins+
"\nPlayer Wins: " + playerWins+
"\n\n Program Terminating", "Results",1);


}

}

最佳答案

Then it decides whether I've won, tied or lost but it won't "reiterate" after that. After that it just says i've either won, lost, or tied 3 times in a row

您实际上所做的是询问输入一次,然后运行循环,因此显然您会得到相同的结果 3 次。

您需要在每次迭代中询问用户输入:

        String player;
String comp;

int ties = 0;
int playerWins = 0;
int compWins = 0;

for(int i = 0; i < 3; i ++){
player = enterPlayersChoice();
comp = generateComputersChoice();
/**/
}

关于java - 剪刀石头布。 java 。方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19960797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com