gpt4 book ai didi

java - 我程序中的无限 do-while 循环 (java)

转载 作者:行者123 更新时间:2023-11-30 08:54:34 25 4
gpt4 key购买 nike

我的第一个 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));

这意味着只要还有剩余的比赛,循环就会重复,或者轮到玩家 1 还剩 1 场比赛并选择 1 场比赛,或者轮到玩家 2 还剩下 1 场比赛并且任选 1 场比赛。

这永远不会发生,因为(除其他原因外)它需要 i%2==0i%2 != 0。将 || 切换为 &&,它应该可以解决问题。正如评论中所指出的,您还需要反转玩家回合检查,因为此时回合计数器已经递增。

你想在这里使用 && 而不是 || 的原因,就像在你的代码的其他地方一样,是你检查一个不同的概念。每隔一段时间,您检查循环应该重复的原因。这一次,您检查循环应该结束的原因,并否定它们。如有疑问,请实际插入比较值,看看它的计算结果是否符合您的预期。

关于java - 我程序中的无限 do-while 循环 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29355226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com