我正在编写一个代码来输出墙上的" 99瓶啤酒"程序。我试图让它在墙上说出一瓶啤酒。"而不是瓶子。我不确定我的代码有什么问题。任何帮助将不胜感激。
public class BeerOnTheWall {
public static void handleCountdown() {
int amount = 99;
int newamt = amount - 1;
String bottles = " bottles";
while(amount != 0) {
if(amount == 1) {
bottles.replace("bottles", "bottle");
}
System.out.println(amount + bottles +" of beer on the wall, "
+ amount + bottles +" of beer! You take one down, pass it around, "
+ newamt + " bottles of beer on the wall!");
amount--;
newamt--;
}
System.out.println("Whew! Done!");
}
public static void main(String args[]) {
handleCountdown();
}
}
我有一个if语句,可以检查int" amount"等于1,然后更换" bottle"与"瓶"。
任何帮助?
谢谢。
答案 0 :(得分:3)
String#replace
会返回修改后的String
,因此您需要替换:
if(amount == 1) {
bottles.replace("bottles", "bottle");
}
使用:
if(amount == 1) {
bottles = bottles.replace("bottles", "bottle");
}