作为输出,“播放器卡”应该随机选择套装和值输出,但不是我的代码。
是否有遗漏或我的if ... else语句搞砸了?我知道我可以使用do ... while,但我希望能够使用if ... else语句。
public static void main(String[] args){
//A new object, player card for the card suit.
Card player = new Card();
//Setting the suit for player card to be displayed randomly
player.setSuit((int) (Math.random()*4));
//Setting the value for player card to be displayed randomly
player.setValue(1 + (int)(Math.random()*12));
//A new object, computer card for the card suit
Card comp = new Card();
//Setting the suit for computer card to be displayed randomly
comp.setSuit((int) (Math.random()*4));
//Setting the value for computer card to be displayed randomly
comp.setValue(1 + (int)(Math.random()*12));
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
else
System.out.println("Tie!");
答案 0 :(得分:2)
这看起来更好:
if(comp.getValue() == player.getValue())
System.out.println("Tie!");
else if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
另外,如果你不使用括号(if { .. } else { .. }
),只有if语句下面的第一行将在if中执行。
答案 1 :(得分:1)
您的代码只会输出播放器和计算机的卡片,如果它们完全相同的话。改变这个:
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
else
System.out.println("Tie!");
对此:
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
// handle redrawing cards here
if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
else
System.out.println("Tie!");
答案 2 :(得分:1)
尝试使用括号来确定每种情况下的内容,这样就不会迷路。
答案 3 :(得分:0)
长if
条件之后的第一行只有在你为玩家和comp获得的随机卡匹配时才会执行,这是不太可能的。你究竟想要实现什么目标?
你应该完全删除那一行(它存在的原因是什么?):
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
答案 4 :(得分:0)
根据我对你的问题的理解,你有错误的条件。正如你现在拥有的那样,只有当计算机和玩家卡完全相同时才会匹配。
尝试类似:
if(comp != null && player != null){
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
else
System.out.println("Tie!");
}
答案 5 :(得分:0)
没有分析太多,但在我看来你想要一些块在里面但是你忘了{ }
制作那些块,不是吗?
答案 6 :(得分:0)
If语句将应用于大括号后面的代码块
if(condition){
thenDoSomething();
thenDoSomethingElse();
}
如果没有花括号,则只有下一个语句将应用于If
if(condition)
thisWillRunIfConditionIsTrue();
thisWillAlwaysRun();
您的代码没有花括号,因此播放器输出只会在与计算机输出匹配时打印,而计算机输出将始终打印。
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
答案 7 :(得分:0)
第一个'if'评估为false,所以行:
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
永远不会调用(除非值和套装都相等)。
您的代码看起来像这样:
if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit())) {
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
}
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
始终使用带有'if'语句的大括号。