我在写Java程序的时候遇到了等号运算符没有好好工作,在正确的位置上应该输出bottle(单数的瓶子)却输出了booles(双数)
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 he 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 he wall");
}else{
System.out.println("No more "+word+" of beer on the wall");
}
}
}
}
这里是代码最后的显示部分
3 bottles of beer on he wall
3 bottles of beer on he wall
3 bottles of beer.
Take one down.
Pass it around.
2 bottles of beer on he wall
2 bottles of beer on he wall
2 bottles of beer.
Take one down.
Pass it around.
1 bottles of beer on he wall
1 bottle of beer on he wall
1 bottle of beer.
Take one down.
Pass it around.
No more bottles of beer on the wall
错了的内容是:
1 bottles of beer on he wall 这里的bottles
1 bottle of beer on he wall
1 bottle of beer.
Take one down.
Pass it around.
No more bottles of beer on the wall 这里的bottles
你这个程序在我本地运行是正常的,你原始代码是不是开了多线程之类的
3 bottles of beer on he wall
3 bottles of beer.
Take one down.
Pass it around.
2 bottles of beer on he wall
2 bottles of beer on he wall
2 bottles of beer.
Take one down.
Pass it around.
1 bottle of beer on he wall
1 bottle of beer on he wall
1 bottle of beer.
Take one down.
Pass it around.
No more bottle of beer on the wall
Process finished with exit code 0