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

时间:2015-03-30 20:01:56

标签: java do-while

我的第一个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场比赛,选手2将选择1或3场比赛。无法再选择比赛的玩家将失去游戏,这意味着有两种结束场景:当没有更多比赛时,或者有1但其他玩家在上一轮比赛中选择了1。

2 个答案:

答案 0 :(得分:1)

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

删除不必要的行:

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

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

您希望在&&使用此||代替{{1}}的原因,就像您代码中的其他位置一样,您需要检查其他概念。每隔一段时间,您都要检查循环应该重复的原因。这一次,您检查循环应该结束的原因,并否定它们。如果有疑问,实际上插入比较值,看看它是否符合你的想法。