试图编写99瓶啤酒歌 [英] Trying to code the 99 bottles of beer song

查看:285
本文介绍了试图编写99瓶啤酒歌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

            if (beerNum == 1) {
                word = "bottle"; // ONE bottle
            }

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + " " + word + " of beer");
            }
        }

        if (beerNum == 0) {
            System.out.println("No more bottles of beer");
        }

    }
}

输出是:

99 bottles of beer on the wall, 99 bottles of beer
Take one down, pass it round 98 bottles of beer
98 bottles of beer on the wall, 98 bottles of beer
Take one down, pass it round 97 bottles of beer
97 bottles of beer on the wall, 97 bottles of beer
Take one down, pass it round 96 bottles of beer
96 bottles of beer on the wall, 96 bottles of beer
Take one down, pass it round 95 bottles of beer
95 bottles of beer on the wall, 95 bottles of beer... 

(And so on and so forth)

3 bottles of beer on the wall, 3 bottles of beer
Take one down, pass it round 2 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer
Take one down, pass it round 1 bottles of beer
1 bottle of beer on the wall, 1 bottle of beer
No more bottles of beer

为什么不是String wo rd等于瓶子?取而代之的是拿下一瓶啤酒,将它传递给1瓶啤酒。

Why isn't the String word equalling "bottle"? Instead it says "bottles" in "Take one down, pass it round 1 BOTTLES of beer.

此外,墙上还有1瓶啤酒,1瓶啤酒它没有说带一个向下通过它

Also after "1 bottle of beer on the wall, 1 bottle of beer" it doesn't say "Take one down pass it round"

链接到歌词

推荐答案

只需把这片你在减去瓶号而不是之前写的代码

Just put this piece of code you wrote after you subtract the bottle number instead of before

if (beerNum == 1) {
    word = "bottle"; //ONE bottle
}

所以你的代码将是

public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

            if (beerNum == 1) {
                word = "bottle"; //ONE bottle
            }

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + " " + word + " of beer");
            }
        }

        if (beerNum == 0) {
            System.out.println("No more bottles of beer");
        }
    }
}

这篇关于试图编写99瓶啤酒歌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆