if (first == 11)
{card1s = "Jack";
}else {
if (second == 11)
{card2s = "Jack";
} else {
if (third == 11)
{card3s = "Jack";
}
else {
if (fourth == 11)
{card4s = "Jack";
}
else {
if (fifth == 11)
{card5s = "Jack";
}
else {
if (first == 12)
{card1s = "Queen";
}
else {
if (second == 12)
{card2s = "Queen";
}
else {
if (third == 12)
{card3s = "Queen";
}
else {
if (fourth == 12)
{card4s = "Queen";
}
else {
if (fifth == 12)
{card5s = "Queen";
}
else {
if (first == 13)
{card1s = "King";
}
else {
if (second == 13)
{card2s = "King";
}
else {
if (third == 13)
{card3s = "King";
}
else {
if (fourth == 13)
{card4s = "King";
}
else {
if (fifth == 13)
{card1s = "King";
}
else {
if (first == 1)
{card1s = "Ace";
}
else {
if (second == 1)
{card2s = "Ace";
}
else {
if (third == 1)
{card3s = "Ace";
}
else {
if (fourth == 1)
{card4s = "Ace";
}
else {
if (fifth == 1)
{card5s = "Ace";
}
else {
card1s = null;
card2s = null;
card3s = null;
card4s = null;
card5s = null;
}}}}}}}}}}}}}}}}}}}}
if ((first >= 11) | (first == 1))
System.out.println("The first card is: " + card1s + first);
这是我的扑克游戏程序的一部分,用于翻译卡片(例如,11是杰克,12是女王),结果应该打印出翻译的单词以及用于确定这个词。该程序编译没有问题,但问题是,当我运行这个程序时,它会提出“第一张卡是:Jack13”或“Queen13”之类的东西。
答案 0 :(得分:0)
您有很多重复的代码,switch
更合适。你也应该使用方法。例如:
public static void main(String[] args) {
// ...
String card1s = getCardAsString(first);
// ...
if ((first >= 11) || (first == 1))
System.out.println("The first card is: " + card1s + first);
}
private static String getCardAsString(int i) {
switch (i) {
case 1:
return "Ace";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return String.valueOf(i); // or return null; not sure what you need
}
}
使用if
s:
private static String getCardAsString2(int i) {
if (i == 1)
return "Ace";
if (i == 11)
return "Jack";
if (i == 12)
return "Queen";
if (i == 13)
return "King";
return String.valueOf(i);// or return null;
}