gpt4 book ai didi

java - 尝试编写 99 瓶啤酒歌曲

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:21 26 4
gpt4 key购买 nike

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

为什么字符串单词不等于“bottle”?取而代之的是“拿一个下来,把它传给 1 瓶啤酒”。

此外,在“墙上有一瓶啤酒,一瓶啤酒”之后,它并没有说“拿一瓶啤酒传下去”

Link to the lyrics .

最佳答案

试试这个代码:

public class BeerSong{
public static void main (String[] args){
int beerNum = 99;
String word = "bottles";
while(beerNum > 0){
if (beerNum == 1){
word = "bottle";
}
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;

if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println("***************************");
}else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}

它将与墙上输出的 1 瓶啤酒一起运行。100% 更正此代码
只需移动 if 语句

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

之后

beerNum = beerNum - 1;

像这样

public class BeerSong{
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");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");

beerNum = beerNum - 1;
if (beerNum == 1){
word = "bottle";
}
if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println("***************************");
}else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}

我使用 System.out.println("************") 因为它会给出清晰的思路当一个循环结束而另一个循环开始时。

关于java - 尝试编写 99 瓶啤酒歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com