如何在值为0时结束循环?

时间:2014-05-29 16:44:07

标签: java loops

如何获得一个无限循环,但在值达到0时结束?我希望这个过程在int money达到0时结束。我不熟悉循环,很抱歉,如果这个问题很愚蠢。

System.out.println(list.get(0).array[3]);

for (int a = 10; a <= 1; a = a - money)
{
    System.out.println("Enter in a vaule you want to bet out of $" + money + "");
    String moneyvalue = Input.next();

    int i = Integer.parseInt(moneyvalue);// i is the user input for
                                            // amount of money they want
                                            // to bet
    int x = money;// total money

    if (i > x)
    {
        System.out.println("Please enter an acceptable value from 1-" + x);
    }

    if (i <= x)
    {

        System.out.println("Which horse do you want to win, enter in 1-5");
        String horsechoice = Input.next();

        int randomhorse = (int) (Math.random() * 5 + 1);
        int horsechoice1 = Integer.parseInt(horsechoice);
        System.out.println(randomhorse);

        if (horsechoice1 == randomhorse)
        {
            System.out.println("You win");
            money = x + i;
            System.out.println("You have $" + money + " remaining.");
        }

        else if (horsechoice1 > 5)
        {
            System.out.println("Sorry try again.");
        }
        else
        {
            System.out.println("You Lose");
            money = x - i;
            System.out.println("You have $" + money + " remaining.");
        }

    }
}

5 个答案:

答案 0 :(得分:4)

你可以在for循环中声明它

for (int a = 10; a <=1, money > 0; a = a - money) {...

答案 1 :(得分:2)

If( money ==0)
   Break;

break语句会让你退出循环 您可以在最后的其他地方放置一个新号码

或者你这样做

for (int a = 10; a <=1, money > 0; a = a - money)

答案 2 :(得分:0)

int x = money;

while(x>0)
{
   // your code here
}

int x;
for(;;)
{
   x = money;
   // your code here
   // ...
   //

   if(x<=0) break;
}

答案 3 :(得分:0)

int money = 1000;//Initialize your value
while(money != 0){
    //do some gambling
}

答案 4 :(得分:0)

我不确定你在for(a ...)循环中做了什么。我想你真的只是想构建以下循环:

while(money > 0)
{
   // do some stuff!
}