简体   繁体   English

同时执行循环

[英]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. 我在下面创建了代码,并在Do-while循环中创建了一个选项,然后不必重新运行该程序就可以进行另一个选择。 The problem is that my do-while loop doesn't work. 问题是我的do-while循环不起作用。 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. 当我运行该程序时,用户的第一选择可以正常工作,但随后程序结束,因此我猜测do-while循环不起作用。

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. 关键是用户应该不可能退出循环,因为while()中作为条件的整数不能更改。 The only way to leave the program should be to type the value 4. 退出程序的唯一方法应该是键入值4。

Can you see what the problem with the do-while loop is? 您能看到do-while循环的问题所在吗?

    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 . end初始化为0 Thus the loop will terminate after the first iteration The loop will only be repeated if end is 1 (which it is not). 因此,循环将在第一次迭代后终止。仅当end1 (不是)时,循环才会重复。

There is no code that sets end equaling one, so while should check for (end == 0) as: 没有代码将end设置为1,因此应检查(end == 0)为:

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 并且在某些地方(可能是在检测到错误时)将shold的一端设置为1,否则,这将是一个无限循环

you can create infinite do while loop, if you want your code execute atleast once. 如果您希望代码至少执行一次,则可以创建无限的do while循环。

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 ). 唯一的问题是,您将end初始化为0,但是循环正在检查它是否为1。您可以通过将声明更改为1( int end = 1 )或更改循环条件( end == 0 )来解决此问题。 。

As you stated, the variable end is never changed. 如您所说,变量end永远不会改变。 The question then comes down to two options. 然后,问题归结为两个选择。 You can use a do-while loop or a while loop. 您可以使用do-while循环或while循环。 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. 但是,如果使用while循环,它将把条件放在第一位,这使读取变得更容易。

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. 如果只想让循环继续进行,就没有理由在这里甚至使用end变量,尤其是在永不更改的情况下。 As stated before, you should use: 如前所述,您应该使用:

while (true) {
    ...
}

Then you can just delete the end variable :-) 然后,您可以删除end变量:-)

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

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