简体   繁体   English

While循环仅循环一次

[英]While loop only loops once

Why does the Coins while loop in the code below only loop once? 为什么下面的代码中的Coins while循环仅循环一次?

The program is supposed to allow the player to take the amount of coins that they want and then if they want more coins can go back. 该程序应该允许玩家取出他们想要的硬币数量,然后如果他们想要更多的硬币可以退回。 For some reason though once you go through the Coins loop and exit it once, regardless of whether or not you got any coins, it no longer works. 出于某种原因,尽管一旦您通过Coins循环并退出一次,无论您是否获得任何硬币,它都将不再起作用。

import java.util.Scanner;
public class Coins
{
    public static void main(String[]args)
    {
        int user;
        int coins=1000;
        int player=0;
        boolean Coins=true;
        boolean whatGame=true;
        while(whatGame)
        {
            System.out.print("Press 1 to get more coins\n");
            Scanner myScan=new Scanner(System.in);
            user=myScan.nextInt();
            if(user==1)
            {
                while(Coins)
                {
                    if((coins>100)||(coins==100))
                    {
                        System.out.print('\u000C');
                        coins=coins-100;
                        player=player+100;
                        System.out.print("Only "+coins+" coins left.\n\n");
                        System.out.print("You now have "+player+" coins.\n\n");
                        System.out.println("Press 1 to get more coins\n\nPress 2 to play another game");
                        user=myScan.nextInt();
                        if(user==1)
                        {
                            System.out.print('\u000C');
                            Coins=true;
                        }
                        else
                        {
                            System.out.print('\u000C');
                            whatGame=true;
                            Coins=false;
                        }
                    }
                    else if((user==1)&&(coins<=0))
                    {
                        System.out.print('\u000C');
                        System.out.print("Sorry no coins left.\n");
                        whatGame=true;
                        Coins=false;
                    }
                }
            }
        }
    }
}

Ok, i don't understand your game... but you set Coins=false inside the loop, then it will never run again. 好的,我不了解您的游戏...但是您在循环内设置了Coins=false ,那么它将永远不会再运行。

If you try something like: 如果您尝试以下操作:

    if(user==1)
    {
        Coins=true;          //new line here
        while(Coins) {

the while loop will start everytime is needed. while循环将在每次需要时启动。

You should reconsider reestructuring your solution. 您应该重新考虑重新构造解决方案。

user=myScan.nextInt(); // from this moment on user may no longer be 1
if(user==1)
{
    System.out.print('\u000C');
    Coins=true;
}
else // thus this branch is executed
{
    System.out.print('\u000C');
    whatGame=true;
    Coins=false; // setting Coins to false, not re-entering loop
}

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

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