card1
和card2
值,运行循环并打印正常,但它只分配RNG值一次,我需要它来打破循环并每次重新分配值,直到有人赢
我试过打破它,但这只会结束整个代码。我不是在寻找答案,只是一些输入和帮助。它应该起到作用,从失败者中扣除一分加入获胜者,除非它是一个平局。然后它运行War并扣除3并添加3(对于使用的卡数量)。一击零,游戏停止并赢得胜利者。
import java.util.Random;
public class warGame {
public static void main(String[] args) {
String p1 = "Player 1";
String p2 = "Player 2";
int s = 10;
int p1Score = s;
int p2Score = s;
Random card = new Random();
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int t = 1;
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
System.out.println("Let's play War!");
//Do while loop that runs the entirety of the program inside.
do {
System.out.println("Turn " + t++ + " -- Player 1's card: " + card1 + " Player 2's card: " + card2);
//determines cards, calculates values and runs proper loop
if (card1 > card2) {
System.out.println("Player 1 wins!");
p1Score += 1;
p2Score -= 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
} else if (card1 == card2) {
System.out.println("Time for war!");
System.out.println("Player 1's war card is: " + war1 + " Player 2's war card is: " + war2);
if (war1 > war2) {
System.out.println("Player 1 wins the war!");
p1Score += 3;
p2Score -= 3;
} else {
System.out.println("Player 2 wins the war!");
p1Score -= 3;
p2Score += 3;
}
} else {
System.out.println("Player 2 wins!");
p1Score -= 1;
p2Score += 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
}
} while (p1Score > 0 && p2Score > 0);
//The following will print out the winner and end the game.
if (p1Score == 0) {
System.out.println("Player 2 wins the game!");
} else {
System.out.println("Player 1 wins the game!");
}
}
}
答案 0 :(得分:0)
实际上简单的回答: 您可以在do-while循环之外分配卡的值。这意味着除非你在循环中更改它们,否则这些值永远不会改变。
答案 1 :(得分:0)
如果您希望循环具有不同的结果,则需要为其提供不同的值。
当你这样做时:
sudo yum install -y docker
您要为这四个变量分配随机值。但是,这些值永远不会改变。
我会用以下代码替换这些行:
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
然后:
int card1;
int card2;
int war1;
int war2;
这次变量'每次迭代时,值都会有所不同。