【发布时间】:2020-10-20 02:43:12
【问题描述】:
我已经创建了这段代码,我正在尝试创建一个循环,让它重复并丢弃 10 张牌,然后如果结果是平局,则显示第 11 张牌,但我不知道如何正确地做到这一点。
public class War {
public static void main(String args[]) {
Card player = new Card();
Card computer = new Card();
final int CARDS_IN_SUIT = 13;
char[] su = {'C', 'H', 'D', 'S'};
char randomSuit = su[(char) (Math.random() * (su.length - 1))];
char randomSuit2 = su[(char) (Math.random() * (su.length - 1))];
int playervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);
int computervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);
System.out.println("Player: " + playervalue + " of " + randomSuit);
player.getValue(randomSuit, playervalue);
System.out.println("Computer: " + computervalue + " of " + randomSuit2);
computer.getValue(randomSuit2, computervalue);
player.setValue(randomSuit, playervalue);
computer.setValue(randomSuit2, computervalue);
if (playervalue > computervalue)
System.out.println("Results: Player won");
else if (computervalue > playervalue)
System.out.println("Results: Computer won");
else
System.out.println("Results: Equal");
}
}
这是类卡片
public class Card {
char suit;
int number;
public void setValue(char su, int num) {
suit = su;
number = num;
if (number <= 1 || number >= 13) {
} else {
number = 1;
}
}
public void getValue(char su, int num) {
switch (su) {
case 'C':
break;
case 'H':
break;
case 'D':
break;
case 'S':
break;
}
}
}
关于如何做或开始的任何建议?我对编码非常陌生,所以这是我的第一个程序。
【问题讨论】:
-
我认为你可以使用 do{}while() :)
标签: java loops for-loop while-loop