Java Class 不会注意到大于 9 的数字

Java Class wont notice numbers over 9

我需要这十个绿瓶 class 从 10 开始下降到 0。但它只从 9 开始,我似乎无法从 10 开始。我想它有与 "int bott_num = 9" 有关。将其设置为 8,7,6... 效果很好!任何超过 9 的东西,它仍然相信它是 9。请帮忙

import java.lang.*;

public class TenGreenBottles
{
    /**
     * This is the main entry point for the application
     */
    public static void main(String args[])
    {
        int bott_num = 9;
        while (bott_num > 2){
            System.out.println(bott_num + " green bottles, hanging on the wall,");
            System.out.println(bott_num + " green bottles, hanging on the wall,");
            System.out.println("and if one green bottle, should accidentally fall,");
            System.out.println("there'll be " + (bott_num - 1) + " green bottles, hanging on the wall");
            System.out.println(" ");

            bott_num = bott_num - 1;
        }

        while  (bott_num > 1){
            System.out.println(bott_num + " green bottles, hanging on the wall,");
            System.out.println(bott_num + " green bottles, hanging on the wall,");
            System.out.println("and if one green bottle, should accidentally fall,");
            System.out.println("there'll be " + (bott_num - 1) + " green bottle, hanging on the wall");
            System.out.println(" ");

            bott_num = bott_num - 1;
        }

        System.out.println(bott_num + " green bottle, hanging on the wall,");
        System.out.println(bott_num + " green bottle, hanging on the wall,");
        System.out.println("and if that green bottle, should accidentally fall,");
        System.out.println("there'll be no green bottles, hanging on the wall");
        System.out.println(" ");
        System.out.println("THE END");
    }

}

您已将 bott_num 定义为

int bott_num = 9;

因此它将以 9 开​​头,但您的打印:

System.out.println("there'll be " + (bott_num - 1) + " green bottles, hanging on the wall");

打印 bott_num - 1,在你的情况下是 8。所以我建议你从 11 开始,这样你会得到如下输出:

 11 green bottles, hanging on the wall,
 ..
 there'll be 10 green bottles, hanging on the wall

虽然如果你想要一致的结果,那么你可以将它初始化为 10 并将你的打印语句更改为:

System.out.println("there'll be " + bott_num + " green bottles, hanging on the wall");

你的第二个 while 循环也将只执行一次,而不是从 10 开始说,因为你在 while 循环 1 中递减 bott_num,所以你可能有分配相同值的临时变量作为 bott_num 并且您递减该变量而不是 bott_num 本身。