Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在我运行程序时数字显示不正确?_Java - Fatal编程技术网

Java 为什么在我运行程序时数字显示不正确?

Java 为什么在我运行程序时数字显示不正确?,java,Java,您将自动播放著名歌曲“99瓶XXX在墙上”。 你将打印这首歌的所有99首诗的歌词。使用循环!如果 你不知道歌词,用谷歌查吧 该计划应: 询问用户的年龄 如果用户是21岁或以上,询问他们是否喜欢啤酒或苏打水 a。如果他们不到21岁或者他们更喜欢苏打水,那么歌词是“99” 墙上有几瓶苏打水“ b。如果他们超过21岁,那就是“99瓶啤酒” 必须使用WHILE循环,并且计数器变量必须是 打印报表 所以第一节是: 墙上有99瓶苏打水 99瓶苏打水 如果其中一个瓶子从墙上掉下来 墙上挂着98瓶苏打水 最后一

您将自动播放著名歌曲“99瓶XXX在墙上”。 你将打印这首歌的所有99首诗的歌词。使用循环!如果 你不知道歌词,用谷歌查吧

该计划应:

询问用户的年龄

如果用户是21岁或以上,询问他们是否喜欢啤酒或苏打水

a。如果他们不到21岁或者他们更喜欢苏打水,那么歌词是“99” 墙上有几瓶苏打水“

b。如果他们超过21岁,那就是“99瓶啤酒”

必须使用WHILE循环,并且计数器变量必须是 打印报表

所以第一节是:

墙上有99瓶苏打水

99瓶苏打水

如果其中一个瓶子从墙上掉下来

墙上挂着98瓶苏打水

最后一节:

墙上有一瓶苏打水

一瓶苏打水

如果那瓶汽水从墙上掉下来

墙上没有汽水瓶

试想一下,你需要在循环中添加什么来打印最后一节 歌词稍有不同

//这是我的密码。当我运行它时,最后的歌词显示如下

1 of beer on the wall
 1 bottles of beer
 If one of those bottles should fall off the wall
 ...0 bottles of beer on the wall
 0 of beer on the wall
 0 bottles of beer
 If that lone bottle of beer should fall off the wall
 No bottles of beer on the wall
//我怎样才能使0瓶零件不出现

Scanner user = new Scanner(System.in);

           int age, beverage;


           System.out.println("Please type in your age");
           age = user.nextInt();

           user.nextLine();
           System.out.println("Would you like soda or beer? soda=1 beer=2");
           beverage = user.nextInt();

        if(age<21 || beverage==1)
           {
           int bottles = 99;
            while( 1<= bottles){
                System.out.println(bottles+" of soda on the wall");
                System.out.println(bottles+" bottles of soda");
                System.out.println("If one of those bottles should fall off the wall");
                bottles--;
                System.out.println("..."+bottles+" bottles of soda on the wall");
            if(bottles==0){
                System.out.println(bottles+" of soda on the wall");
                System.out.println(bottles+" bottles of soda");
                System.out.println("If that lone bottle of soda should fall off the wall");
                System.out.println("No bottles of soda on the wall");
            }
        }
    }
        if(age>=21 && beverage == 2)
        {
            int bottles=99;
            while(1<= bottles){
                System.out.println(bottles+" of beer on the wall");
                System.out.println(bottles+" bottles of beer");
                System.out.println("If one of those bottles should fall off the wall");
                bottles--;
                System.out.println("..."+bottles+" bottles of beer on the wall");
            if(bottles==0){
                System.out.println(bottles+" of beer on the wall");
                System.out.println(bottles+" bottles of beer");
                System.out.println("If that lone bottle of beer should fall off the wall");
                System.out.println("No bottles of beer on the wall");
            }
        }
    }

        }
    }

我相信你已经改变了循环测试的条件。另外,我将使用printf实现这一点。然后,您可以在格式中包含饮料。如果我像这样运行它,它就会工作

public static void main(String[] args) {
    Scanner user = new Scanner(System.in);
    int bottles = 99;

    System.out.println("Please type in your age");
    int age = user.nextInt();
    System.out.println("Would you like soda or beer? soda=1 beer=2");
    int beverage = user.nextInt();
    String drink = (age >= 21 && beverage == 2) ? "beer" : "soda";
    while (bottles > 1) { //  <-- swap 1 and bottles.
        System.out.printf("%d bottles of %s on the wall%n", bottles, drink);
        System.out.printf("%d bottles of %s%n", bottles, drink);
        System.out.println("If one of those bottles "
                + "should fall off the wall");
        bottles--;
        System.out.printf("... %d bottles of %s on the wall.%n", bottles,
                drink);
    }
    System.out.printf("%d bottle of %s on the wall%n", 1, drink);
    System.out.printf("%d bottle of %s%n", 1, drink);
    System.out.printf("If that lone bottle of %s should fall off the wall%n",
                drink);
    System.out.printf("No bottles of %s on the wall!%n", drink);
}

@HyunseokSong的一个要求是,只有当用户21岁或以上时,才向他们询问苏打水或啤酒。如果他们不到21岁或被要求喝苏打水,请出示苏打水。@SJuan76你能详细解释一下吗?我是针对你原来的问题评论的,不是关于你编辑的问题。请避免把一个问题变成另一个问题,因为人们正在帮助你;如果你有更多的问题,将正确的答案标记为已接受,然后发布一个不同的问题。这不会有什么区别。为什么会被接受?它真的做了什么吗?
 if(age>=21 && beverage==2)
       int bottles = 99; /* initialize bottles here*/