有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我的程序中的无限dowhile循环(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场


共 (2) 个答案

  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。将||切换到&&,应该可以解决问题。正如评论中指出的,你还需要反转玩家的回合检查,因为回合计数器已经增加了这个点

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

  2. # 2 楼答案

    比赛结束时,没有比赛了。所以while(totalMatches > 0);就足够了

    删除不必要的行:

    || !(!(i % 2 == 0) && totalMatches == 1 && matchesPlayer1 == 1) || !((i % 2 == 0) && totalMatches == 1 && matchesPlayer2 == 1));