简体   繁体   English

当条件为假时,Do-While 循环不会终止

[英]Do-While loop doesn't terminate when condition is false

This is a game where you can take biscuits from barrels, either from barrel1, barrel2, or both.这是一款您可以从桶中取出饼干的游戏,可以从桶 1、桶 2 或两者中取出。 The last player to take the last biscuits wins the game.最后拿走最后一块饼干的玩家获胜。 I implemented the game in a do-while loop so that it loops every turn.我在 do-while 循环中实现了游戏,以便它每回合循环一次。 However, once the number of biscuits in both barrels = 0, the loop doesn't terminate and keeps on taking scanner input.但是,一旦两个桶中的饼干数量 = 0,循环就不会终止并继续接受扫描仪输入。

NB This is coursework for university, so please do not tell me exactly what to do or give me exact code, just suggestions or why my code is not working.注意这是大学的课程,所以请不要确切地告诉我要做什么或给我确切的代码,只是建议或为什么我的代码不起作用。

import java.util.Scanner;

public class LastBiscuit {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int barrel1 = 6;
        int barrel2 = 8;

        // Simple turn counter. incremented every loop. if even, player 2 turn
        int turnCounter = 0;

        do {
            turnCounter++;
            int howMany = 20;
            String turnAction;
            // prints out biscuits left in each barrel
            String output1 = String.format("Biscuits Left - Barrel 1: %d",barrel1);
            String output2 = String.format("Biscuits Left - Barrel 2: %d",barrel2);
            System.out.println(output1);
            System.out.println(output2);

            // Check turn counters value. If even, it is player 2 turn, else player 1 turn.
            if (turnCounter % 2 == 0) {
                System.out.println("Player Turn: 2");
            }
            else {
                System.out.println("Player Turn: 1");
            }
            
            // Player picks what action to take in their turn. Stored in turnAction. Only allows correct input
            System.out.print("Choose a barrel: barrel1 (one), barrel2 (two), or both (both), or skip turn (skip)?");
            do {
            turnAction = in.next();
            } while (!turnAction.equalsIgnoreCase("one") && !turnAction.equalsIgnoreCase("two") &&
                !turnAction.equalsIgnoreCase("both") && !turnAction.equalsIgnoreCase("skip"));

            // Player picks how many biscuits to take, if at all. If biscuits taken larger than biscuits remaining,
            // they have to re-enter integer
            if (!(turnAction.equalsIgnoreCase("skip"))) {
                System.out.print(" How many biscuits are you taking?");
                while(!in.hasNextInt()) {
                    System.out.println("Try again");
                    in.next();
                }
                howMany = in.nextInt();
                while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
                    howMany = in.nextInt();

                }
            }

            // Takes biscuits from barrels chosen
            if (turnAction.equalsIgnoreCase("one")) {
                barrel1 -= howMany;
            }
            if (turnAction.equalsIgnoreCase("two")) {
                barrel2 -= howMany;
            }
            if (turnAction.equalsIgnoreCase("both")) {
                barrel1 -= howMany;
                barrel2 -= howMany;
            }
            // do nothing on skip


        } while (barrel1 > 0 || barrel2 > 0);

        //bug? doesnt print? outside of do-while loop
        // doesnt exit loop?
        System.out.println("YOYYYOOYOY");

        if (turnCounter % 2 == 0) {
            System.out.println("Player 2 Wins! ");
        }
        else {
            System.out.println("Player 1 Wins! ");
        }
    }
}

You have an infinite while loop running at:你有一个无限的 while 循环运行在:

while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
    howMany = in.nextInt();
}

This only happens when you require more biscuits than a barrel contains or you request a negative number of biscuits.这只发生在您需要的饼干多于桶装的饼干或您要求的饼干数量为负数时。 When it does you will be forever in this while loop.当它发生时,您将永远处于此 while 循环中。 Place a breakpoint at howMany = in.nextInt();howMany = in.nextInt();处放置一个断点as already suggested and you will see it happening.正如已经建议的那样,你会看到它发生。

只需运行 Debug 并尝试遵循逻辑,您就会明白这一切......

Fix infinite loop and the condition makes no sense.修复无限循环,条件没有意义。 May be something like this可能是这样的

boolean isNextInputNecessary = ((barrel1 + barrel2) - howMany) < 0 || howMany <= 0;
while (isNextInputNecessary) {
    howMany = in.nextInt();
}

or you need to check one , two or both in condition.或者您需要检查onetwoboth的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM