希望这是一个快速的问题。我正在制作一个简单的石头剪刀游戏。我做一个随机的计算机选择,并得到用户的选择。但是当我试图找到谁获胜时,如果用于无效输入的块,它会在我的其他地方打印最后一个。
在做出正确选择后,它会打印“输入有效选项”。
import java.util.Random;
import javax.swing.JOptionPane;
public class JavaApplication4 {
public static void main(String[] args)
{
Random ranNums = new Random();
int comp = ranNums.nextInt(3);
String comp2;
String winner;
String user = JOptionPane.showInputDialog
(null, "Enter rock, paper, or scissors");
user.toLowerCase();
if(comp == 0)
comp2 = "rock";
else if(comp == 1)
comp2 = "paper";
else
comp2 = "scissors";
//Computer wins
if(comp2 == "rock" && user == "scissors")
winner = "The computer wins";
else if(comp2 == "paper" && user == "rock")
winner = "The computer wins";
else if(comp2 == "scissors" && user == "paper")
winner = "The computer wins";
//Tie game
else if(comp2 == "rock" && user == "rock")
winner = "It's a tie";
else if(comp2 == "paper" && user == "paper")
winner = "It's a tie";
else if(comp2 == "scissors" && user == "scissors")
winner = "It's a tie";
//User wins
else if(comp2 == "scissors" && user == "rock")
winner = "You win!";
else if(comp2 == "rock" && user == "paper")
winner = "You win!";
else if(comp2 == "paper" && user == "scissors")
winner = "You win!";
else
winner = "Enter a valid choice";
JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
"The computer picked " + comp2 + "\n" +
winner);
}
}
答案 0 :(得分:4)
你不能使用
comp == "paper"
比较java中的字符串。你用
comp.equals("paper")
或如果情况无关
comp.equalsIgnoreCase("paper")
答案 1 :(得分:3)
不要使用==
来比较字符串。请改用equals或equalsIgnoreCase(...)方法。理解==检查两个对象是否相同,而不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这才是最重要的。而不是
if (fu == "bar") {
// do something
}
做,
if ("bar".equals(fu)) {
// do something
}
,或者
if ("bar".equalsIgnoreCase(fu)) {
// do something
}
简化代码的一种方法是创建一个RockPaperScissors枚举并在比较方法上给它。类似的东西:
enum RockPaperScissors {
ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors");
private String text;
private static int[][] winMatrix = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}};
private RockPaperScissors(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
// can't use compareTo since it is a final method for enums
public int compareVs(RockPaperScissors other) {
int thisOrdinal = ordinal();
int otherOrdinal = other.ordinal();
return winMatrix[thisOrdinal][otherOrdinal];
}
}
然后比较一个枚举与另一个枚举,只需调用其compareVs(...)方法传入另一个枚举。
所以你的巨大if / else块会降低到:
// assuming that user and comp are RockPaperScissors variables
int result = user.compareVs(comp);
if (result == 1) {
System.out.println("You've won!");
} else if (result == 0) {
System.out.println("It's a tie!");
} if (result == -1) {
System.out.println("You've lost!");
}
答案 2 :(得分:2)
如果你像我一样,你喜欢原始代码:
import java.util.Random;
import javax.swing.JOptionPane;
public class JavaApplication4 {
public static void main(String[] args)
{
Random ranNums = new Random();
int comp = ranNums.nextInt(3);
String comp2;
String winner;
String user = JOptionPane.showInputDialog
(null, "Enter rock, paper, or scissors");
user.toLowerCase();
if(comp == 0)
comp2 = "rock";
else if(comp == 1)
comp2 = "paper";
else
comp2 = "scissors";
//Computer wins
if(comp2.equals("rock") && user.equals( "scissors"))
winner = "The computer wins";
else if(comp2.equals("paper") && user.equals( "rock"))
winner = "The computer wins";
else if(comp2.equals("scissors") && user.equals( "paper"))
winner = "The computer wins";
//Tie game
else if(comp2.equals("rock") && user.equals( "rock"))
winner = "It's a tie";
else if(comp2.equals("paper") && user.equals( "paper"))
winner = "It's a tie";
else if(comp2.equals("scissors") && user.equals( "scissors"))
winner = "It's a tie";
//User wins
else if(comp2.equals("scissors") && user.equals( "rock"))
winner = "You win!";
else if(comp2.equals("rock") && user.equals( "paper"))
winner = "You win!";
else if(comp2.equals("paper") && user.equals( "scissors"))
winner = "You win!";
else
winner = "Enter a valid choice";
JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
"The computer picked " + comp2 + "\n" +
winner);
}
}
希望有所帮助!
答案 3 :(得分:1)
您无法比较这样的字符串:if (comp2 == "rock")
您需要写:if ("rock".equals(comp2))