我假设这是一个逻辑错误。我无法得到结果,我无法得到正确的结果。每当我进入摇滚,纸张,剪刀时,它就会从那里决定我是赢了,输了还是打了。我的代码出了什么问题?
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);
}
}
答案 0 :(得分:2)
然后它决定我是赢了,并列还是输了但是不会 之后“重申”。在那之后,它只是说我要么赢了,输了, 或连续3次绑定
你实际做的是询问输入一次然后运行循环,所以显然你会得到相同的结果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();
/**/
}
答案 1 :(得分:1)
首先,你必须得到玩家的选择和每个循环的计算机选择。移动
String player = enterPlayersChoice();
String comp = generateComputersChoice();
在开始的for
循环内。
另外,如果你说
else if(player.equals("rock")){
if(comp.equals("scissors")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;
}
当玩家选择“摇滚”时,else if
块将匹配,无论计算机的选择如何。然后,如果计算机没有选择“剪刀”,则不会发生任何事情。
您需要两个条件来匹配此方案才能执行,否则第一个条件将匹配,但第二个条件可能不匹配,并且不会发生任何事情。尝试
else if (player.equals("rock") && comp.equals("scissors")){
同样适用于其他条件。
答案 2 :(得分:0)
每次循环都会重新生成用户和计算机的选择。 移动
String player = enterPlayersChoice();
String comp = generateComputersChoice();
进入循环,否则它将是相同的所有3次