输出请参考图片。我的代码能够组织适当的分数,然后根据条件选择获胜者。获胜者不得超过 21,并且获胜者不能出现平局。如果有 21 & 21 那么没有人赢。但是,如果有 24 24 21 19,则 21 获胜。 (添加 bc 堆栈需要更多细节)
如果有人能指出为什么我的程序打印出 4 个同一个获胜者的陈述,我将不胜感激。
This is my code for the section:
/**
* Determines who won the game, and prints the results.
*/
public void printWinner() {
// Students: your code goes here.
int[] arr = {this.human.getScore(), this.player1.getScore(), this.player2.getScore(), this.player3.getScore()};
Arrays.sort(arr);
for (int num : arr) {
if (arr[3] != arr[2] && arr[3] <= 21) {
if (arr[3] == human.getScore()) {
System.out.println(human.name + " wins with " + arr[3] + " points!");
}
if (arr[3] == player1.getScore()) {
System.out.println(player1.name + " wins with " + arr[3] + " points!");
}
if (arr[3] == player2.getScore()) {
System.out.println(player2.name + " wins with " + arr[3] + " points!");
}
if (arr[3] == player3.getScore()) {
System.out.println(player3.name + " wins with " + arr[3] + " points!");
}
}
else if (arr[3] == arr[2] && arr[3] <= 21) {
System.out.println("Tie, nobody wins.");}
else if (arr[3] == arr[2] && arr[3] > 21) {
if (arr [1] <= 21 && arr[1] != arr[0]) {
if (arr[1] == human.getScore()) {
System.out.println(human.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player1.getScore()) {
System.out.println(player1.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player2.getScore()) {
System.out.println(player2.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player3.getScore()) {
System.out.println(player3.name + " wins with " + arr[1] + " points!");
}
}
if (arr [1] <= 21 && arr[1] == arr[0]) {
System.out.println("Tie, nobody wins.");
}
}
else if (arr[3] > 21 && arr[2] > 21) {
if (arr[1] == human.getScore()) {
System.out.println(human.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player1.getScore()) {
System.out.println(player1.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player2.getScore()) {
System.out.println(player2.name + " wins with " + arr[1] + " points!");
}
if (arr[1] == player3.getScore()) {
System.out.println(player3.name + " wins with " + arr[1] + " points!");
}
else if (arr[3] >21 && arr[2] <= 21) {
if (arr [2] != arr [1]) {
if (arr[2] == human.getScore()) {
System.out.println(human.name + " wins with " + arr[2] + " points!");
}
if (arr[2] == player1.getScore()) {
System.out.println(player1.name + " wins with " + arr[2] + " points!");
}
if (arr[2] == player2.getScore()) {
System.out.println(player2.name + " wins with " + arr[2] + " points!");
}
if (arr[2] == player3.getScore()) {
System.out.println(player3.name + " wins with " + arr[2] + " points!");
}
}
if (arr [2] == arr [1]) {
System.out.println("Tie, nobody wins.");}
}
else if (arr[1] >21 && arr[0] <= 21) {
if (arr[0] == human.getScore()) {
System.out.println(human.name + " wins with " + arr[0] + " points!");
}
if (arr[0] == player1.getScore()) {
System.out.println(player1.name + " wins with " + arr[0] + " points!");
}
if (arr[0] == player2.getScore()) {
System.out.println(player2.name + " wins with " + arr[0] + " points!");
}
if (arr[0] == player3.getScore()) {
System.out.println(player3.name + " wins with " + arr[0] + " points!");
}}
else if (arr[0] > 21) {
System.out.println("Nobody wins.");
}
}
}
}}
在检测获胜者方面效果很好,唯一的问题是同一个指定的获胜者打印了 4 次。
我只是不确定我需要编辑什么才能只打印一次结果。