简体   繁体   中英

Do-while loop that won't work

I've created the code below and a Do-while loop to make it possible to do one choice and then do another without having to rerun the program. The problem is that my do-while loop doesn't work. When I run the program the users first choice works fine, but then the programs ends, so I'm guessing that the do-while loop doesn't work.

The point is that it should be impossible for the user to leave the loop, because the integer that is the condition in the while () is not possible to change. The only way to leave the program should be to type the value 4.

Can you see what the problem with the do-while loop is?

    int value = 0;
    int end = 0;

    System.out.println("Välkommen!");

    do
    {       
        value = menu(choice);

        if (value == 1)
        {
            System.out.println();
            System.out.println("Hur mycket vill du sätta in?");

            amount = in.nextInt();

            if ( amount > 0 )
            {           
                makeTransaction(trans, amount);
                System.out.println();
                System.out.println("Du har gjort en insättning på " + amount + " SEK.");
            }

            else if ( amount == 0 )
            {
                System.out.println("Du har ändrat dig, ingen transaktion görs.");
            }

            else if ( amount < 0)
            {
                System.out.println();
                System.out.println("Error, endast positiva heltal är möjliga att mata in! Ingen transaktion görs");
            }
        }

        else if (value == 2)
        {
            System.out.println();
            System.out.println("Hur mycket vill du ta ut?");

            amount = in.nextInt();

            if ( amount > 0 && amount <= balance )
            {

                makeTransaction(trans, amount);
                System.out.println();
                System.out.println("Du har gjort ett uttag på " + (amount * -1) + " SEK.");

            }

            else if ( amount > 0 && amount > balance )
            {
                System.out.println();
                System.out.println("Täckning saknas!");
            }

            else if ( amount == 0 )
            {
                System.out.println();
                System.out.println("Du har ändrat dig, ingen transaktion görs.");
            }

            else if ( amount < 0)
            {
                System.out.println();
                System.out.println("Error, endast positiva heltal är möjliga att mata in! Ingen transaktion görs");
            }
        }

        else if (value == 3)
        {
            balance = IntStream.of(trans).sum();
            showTransactions(trans, balance);
        }

        else if (value == 4)
        {
            System.out.println();
            System.out.print("Tack för idag, glöm inte bankomatkortet!");
            System.exit(0);
        }
    } 
    while (end == 1);

Instead of:

while (end == 1);

It should be:

while (end == 0);

Make it easy on yourself - instead of do-while, just use:

while(true) {
    ...
}

end is initialized to 0 . Thus the loop will terminate after the first iteration The loop will only be repeated if end is 1 (which it is not).

There is no code that sets end equaling one, so while should check for (end == 0) as:

do {
...
} while (end == 0);

and also there shold be somewhere (may be when you detect an error) end should be set to 1, otherwise, this will be an infinite loop

you can create infinite do while loop, if you want your code execute atleast once.

do{

}while(true)

Your only issue is that you initialized end to be 0, yet your loop is checking if it is 1. You can fix this by changing the declaration to 1 ( int end = 1 ) or you change the loop condition ( end == 0 ).

As you stated, the variable end is never changed. The question then comes down to two options. You can use a do-while loop or a while loop. Since you do not care if it has to execute at least once, I would say either is fine. However, if you use the while loop, it puts the condition first, which makes for easier reading.

If you want to just keep the loop going, there is no reason to even use the end variable here, especially if it is never changed. As stated before, you should use:

while (true) {
    ...
}

Then you can just delete the end variable :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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