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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
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
Run Code Online (Sandbox Code Playgroud)
为什么String字不等于"瓶子"?相反,它在"拿下一个,将它传递给1个瓶子的啤酒"中说"瓶子".
在"墙上一瓶啤酒,一瓶啤酒"之后,它也没有说"带一个下来通过它"
小智 5
试试这段代码:
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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它将与墙上的1瓶啤酒一起运行.要更正此代码100%
只需移动if语句即可
beerNum = beerNum - 1;
if (beerNum == 1){
word = "bottle";
}
Run Code Online (Sandbox Code Playgroud)
后
beerNum = beerNum - 1;
Run Code Online (Sandbox Code Playgroud)
像这样
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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用System.out.println("************")它,因为它会在一个循环结束和其他开始时给出一个清晰的想法.
Ran*_*ot6 -1
只需将你写的这段代码放在减去瓶子编号之后而不是之前
if (beerNum == 1) {
word = "bottle"; //ONE bottle
}
Run Code Online (Sandbox Code Playgroud)
所以你的代码将是
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8776 次 |
| 最近记录: |