【发布时间】:2015-08-13 14:34:47
【问题描述】:
我正在努力做到这一点,所以当我的两个或多个 if 语句起作用时,它们不会都打印,而是只会说两个在正确的位置或更多它更有效。我不确定我是否应该做更多的 if 语句,如果我应该使用另一个语句或 forloop。
//If Statements
if (Gameboard[0] == Secret_code[0]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[1] == Secret_code[1]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[2] == Secret_code[2]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[3] == Secret_code[3]) {
System.out.println ("You have one in the correct spot");
}
}
}
【问题讨论】:
-
至少,对于某些
i,您的代码似乎可以简化为if (Gameboard[i] == Secret_code[i]) { System.out.println ("You have one in the correct spot"); },例如在for循环内。 -
我建议先计算点击次数,然后根据该计数计算输出。
标签: java if-statement bluej