因此,我必须制作一个游戏,分别比较两个int数组元素,看看一个是否大于另一个int数组,或者它们是否相等,并打印一条消息,声明哪个玩家获胜或是否平局。两名选手都获得了7张牌,因此必须在不同回合中将它们进行7次比较。在比较每个我无法得到的数字时,我陷入了困境,我真的不知道如何做到这一点,因此每次迭代都声明必须进行7次新回合吗?经过多次尝试,这就是我目前拥有的。任何帮助将不胜感激!
public class Exercise1 {
public static void main(String[] args) {
int[] playerOne = new int[6];
int[] playerTwo = new int [6];
playerOne[0] = 10;
playerOne[1] = 6;
playerOne[2] = 8;
playerOne[3] = 9;
playerOne[4] = 7;
playerOne[5] = 12;
playerOne[6] = 7;
playerTwo[0] = 7;
playerTwo[1] = 6;
playerTwo[2] = 9;
playerTwo[3] = 5;
playerTwo[4] = 2;
playerTwo[5] = 8;
playerTwo[6] = 11;
for (int i = 0; i <= playerOne.length - 1; i++) {
if (playerOne[i] < playerTwo) {
}
您需要2个变量来保持每个人的得分,增加每个回合中具有较大卡牌的变量,最后找到最大得分
int[] playerOne = {10, 6, 8, 9, 7, 12, 7};
int[] playerTwo = {7, 6, 9, 5, 2, 8, 12};
int scorePlayerOne = 0, scorePlayerTwo = 0;
for (int i = 0; i <= playerOne.length - 1; i++) {
if (playerOne[i] < playerTwo[i]) {
scorePlayerTwo++;
} else if (playerTwo[i] < playerOne[i]) {
scorePlayerOne++;
}
}
if (scorePlayerOne < scorePlayerTwo) {
System.out.println("Player Two wins");
} else if (scorePlayerTwo < scorePlayerOne) {
System.out.println("Player One wins");
} else {
System.out.println("Draw");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句