我程序中的无限 do-while 循环 (java)
Infinite do-while loop in my program (java)
我在 Java 中的第一个程序有问题。我正在尝试重现你玩火柴的游戏,但程序永远不会在它应该停止的时候停止...
当我输入 6、3、2、1 或 7、3、2、1 之类的数字时,循环应该停在那里,但它只是继续到下一个回合,就好像什么都没发生一样,即使变量有正确的值并且应该匹配结束条件。
我很确定问题出在主循环的 while 部分(在最后),但我看不到它!这可能是显而易见的,但是...
这里是完整的源代码(游戏规则在下面):
import java.util.Scanner;
public class JeuDeNim {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Starting the game
int totalMatches;
do {
System.out.println("How many matches do you want to play with? "
+ "(from 6 to 60)");
totalMatches = sc.nextInt();
}
while(totalMatches < 6 || totalMatches > 60);
int matchesPlayer1 = 0;//to keep track of
int matchesPlayer2 = 0;//the previous round
int i = 1;//to know whose turn it is
do{
//player 1
if(!(i % 2 == 0)) {//if odd number, player 1
//round 1
if(i == 1) {
do {
System.out.println("Player 1: How many matches do you "
+ "want to pick? (1, 2 or 3)");
matchesPlayer1 = sc.nextInt();
}
while(matchesPlayer1 < 1 || matchesPlayer1 > 3);
totalMatches = totalMatches - matchesPlayer1;
i++;
}
//odd round x
else {
do {
System.out.println("Player 1: How many matches do you "
+ "want to pick this turn?");
matchesPlayer1 = sc.nextInt();
if(totalMatches - matchesPlayer1 < 0) {
System.out.println("Pick a smaller number");
//totalMatches cannot be negative
} else if(matchesPlayer1 == matchesPlayer2) {
System.out.println("You cannot pick the same number "
+ "of matches as Player 2");
}
}
while(matchesPlayer1 < 1 || matchesPlayer1 > 3
|| (totalMatches - matchesPlayer1 < 0)
|| (matchesPlayer1 == matchesPlayer2));
totalMatches = totalMatches - matchesPlayer1;
if(totalMatches == 0
|| (totalMatches == 1 && matchesPlayer1 == 1)) {
System.out.println("Player 1 Wins!");
}
i++;
}
}
//player 2
else {
//round 2
if(i == 2) {
do {
System.out.println("Player 2: How many matches do you "
+ "want to pick? (1, 2 or 3)");
matchesPlayer2 = sc.nextInt();
if(matchesPlayer2 == matchesPlayer1) {
System.out.println("You cannot pick the same "
+ "number of matches as Player 2");
}
}
while(matchesPlayer2 < 1 || matchesPlayer2 > 3
|| matchesPlayer2 == matchesPlayer1);
totalMatches = totalMatches - matchesPlayer2;
i++;
}
//even round x
else {
do {
System.out.println("Player 2: How many matches do you "
+ "want to pick this turn?");
matchesPlayer2 = sc.nextInt();
if (totalMatches - matchesPlayer2 < 0) {
System.out.println("Pick a smaller number");
//totalMatches cannot be negative
} else if(matchesPlayer2 == matchesPlayer1) {
System.out.println("You cannot pick the same number "
+ "of matches as Player 1");
}
}
while(matchesPlayer2 < 1 || matchesPlayer2 > 3
|| (totalMatches - matchesPlayer2 < 0)
|| (matchesPlayer2 == matchesPlayer1));
totalMatches = totalMatches - matchesPlayer2;
if(totalMatches == 0
|| (totalMatches == 1 && matchesPlayer2 == 1)) {
System.out.println("Player 2 Wins!");
}
i++;
}
}
System.out.println("totalMatches: " + totalMatches + " "
+ "matchesPlayer1: " + matchesPlayer1 + " " + "matchesPlayer2: "
+ matchesPlayer2);//to check that everything is working. It is not...
}
while(totalMatches > 0
|| !(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1)
|| !((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
}
}
以下是游戏规则:这是一款双人游戏,玩家轮流选择火柴(1、2 或 3)并且不能选择与另一玩家相同数量的火柴:如果玩家一选择 2 场比赛,玩家二将不得不选择 1 场或 3 场比赛。不能再选择匹配的玩家输掉游戏,这意味着有两种结束情况:当没有更多匹配时,或者有 1 个但另一个玩家在上一轮中选择了 1 个。
当没有更多比赛剩余时,游戏结束。所以 while(totalMatches > 0);
就够了。
删除不需要的行:
|| !(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1)
|| !((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
查看最终 while
循环中的条件
totalMatches > 0 ||
!(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1) ||
!((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
这意味着只要还有剩余的比赛,循环就会重复,或者轮到玩家 1 还剩 1 场比赛并选择 1 场比赛,或者轮到玩家 2 还剩 1 场比赛离开并选择 1 场比赛。
这永远不会发生,因为(除其他原因外)它需要 i%2==0
和 i%2 != 0
。将 ||
切换为 &&
,应该可以解决问题。正如评论中指出的那样,您还需要反转玩家回合检查,因为此时回合计数器已经递增。
您想在这里使用 &&
而不是 ||
的原因,就像在您代码的其他地方一样,是因为您要检查不同的概念。每隔一段时间,您检查循环应该重复的原因。这一次,您检查循环应该结束的原因,并否定它们。如有疑问,请实际插入比较值,看看它是否符合您的预期。
我在 Java 中的第一个程序有问题。我正在尝试重现你玩火柴的游戏,但程序永远不会在它应该停止的时候停止...
当我输入 6、3、2、1 或 7、3、2、1 之类的数字时,循环应该停在那里,但它只是继续到下一个回合,就好像什么都没发生一样,即使变量有正确的值并且应该匹配结束条件。
我很确定问题出在主循环的 while 部分(在最后),但我看不到它!这可能是显而易见的,但是...
这里是完整的源代码(游戏规则在下面):
import java.util.Scanner;
public class JeuDeNim {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Starting the game
int totalMatches;
do {
System.out.println("How many matches do you want to play with? "
+ "(from 6 to 60)");
totalMatches = sc.nextInt();
}
while(totalMatches < 6 || totalMatches > 60);
int matchesPlayer1 = 0;//to keep track of
int matchesPlayer2 = 0;//the previous round
int i = 1;//to know whose turn it is
do{
//player 1
if(!(i % 2 == 0)) {//if odd number, player 1
//round 1
if(i == 1) {
do {
System.out.println("Player 1: How many matches do you "
+ "want to pick? (1, 2 or 3)");
matchesPlayer1 = sc.nextInt();
}
while(matchesPlayer1 < 1 || matchesPlayer1 > 3);
totalMatches = totalMatches - matchesPlayer1;
i++;
}
//odd round x
else {
do {
System.out.println("Player 1: How many matches do you "
+ "want to pick this turn?");
matchesPlayer1 = sc.nextInt();
if(totalMatches - matchesPlayer1 < 0) {
System.out.println("Pick a smaller number");
//totalMatches cannot be negative
} else if(matchesPlayer1 == matchesPlayer2) {
System.out.println("You cannot pick the same number "
+ "of matches as Player 2");
}
}
while(matchesPlayer1 < 1 || matchesPlayer1 > 3
|| (totalMatches - matchesPlayer1 < 0)
|| (matchesPlayer1 == matchesPlayer2));
totalMatches = totalMatches - matchesPlayer1;
if(totalMatches == 0
|| (totalMatches == 1 && matchesPlayer1 == 1)) {
System.out.println("Player 1 Wins!");
}
i++;
}
}
//player 2
else {
//round 2
if(i == 2) {
do {
System.out.println("Player 2: How many matches do you "
+ "want to pick? (1, 2 or 3)");
matchesPlayer2 = sc.nextInt();
if(matchesPlayer2 == matchesPlayer1) {
System.out.println("You cannot pick the same "
+ "number of matches as Player 2");
}
}
while(matchesPlayer2 < 1 || matchesPlayer2 > 3
|| matchesPlayer2 == matchesPlayer1);
totalMatches = totalMatches - matchesPlayer2;
i++;
}
//even round x
else {
do {
System.out.println("Player 2: How many matches do you "
+ "want to pick this turn?");
matchesPlayer2 = sc.nextInt();
if (totalMatches - matchesPlayer2 < 0) {
System.out.println("Pick a smaller number");
//totalMatches cannot be negative
} else if(matchesPlayer2 == matchesPlayer1) {
System.out.println("You cannot pick the same number "
+ "of matches as Player 1");
}
}
while(matchesPlayer2 < 1 || matchesPlayer2 > 3
|| (totalMatches - matchesPlayer2 < 0)
|| (matchesPlayer2 == matchesPlayer1));
totalMatches = totalMatches - matchesPlayer2;
if(totalMatches == 0
|| (totalMatches == 1 && matchesPlayer2 == 1)) {
System.out.println("Player 2 Wins!");
}
i++;
}
}
System.out.println("totalMatches: " + totalMatches + " "
+ "matchesPlayer1: " + matchesPlayer1 + " " + "matchesPlayer2: "
+ matchesPlayer2);//to check that everything is working. It is not...
}
while(totalMatches > 0
|| !(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1)
|| !((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
}
}
以下是游戏规则:这是一款双人游戏,玩家轮流选择火柴(1、2 或 3)并且不能选择与另一玩家相同数量的火柴:如果玩家一选择 2 场比赛,玩家二将不得不选择 1 场或 3 场比赛。不能再选择匹配的玩家输掉游戏,这意味着有两种结束情况:当没有更多匹配时,或者有 1 个但另一个玩家在上一轮中选择了 1 个。
当没有更多比赛剩余时,游戏结束。所以 while(totalMatches > 0);
就够了。
删除不需要的行:
|| !(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1)
|| !((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
查看最终 while
循环中的条件
totalMatches > 0 ||
!(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1) ||
!((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));
这意味着只要还有剩余的比赛,循环就会重复,或者轮到玩家 1 还剩 1 场比赛并选择 1 场比赛,或者轮到玩家 2 还剩 1 场比赛离开并选择 1 场比赛。
这永远不会发生,因为(除其他原因外)它需要 i%2==0
和 i%2 != 0
。将 ||
切换为 &&
,应该可以解决问题。正如评论中指出的那样,您还需要反转玩家回合检查,因为此时回合计数器已经递增。
您想在这里使用 &&
而不是 ||
的原因,就像在您代码的其他地方一样,是因为您要检查不同的概念。每隔一段时间,您检查循环应该重复的原因。这一次,您检查循环应该结束的原因,并否定它们。如有疑问,请实际插入比较值,看看它是否符合您的预期。