首页 / JAVA / 试着学习JAVA,我想写99瓶啤酒歌
试着学习JAVA,我想写99瓶啤酒歌
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了试着学习JAVA,我想写99瓶啤酒歌,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1553字,纯文字阅读大概需要3分钟。
内容图文
![试着学习JAVA,我想写99瓶啤酒歌](/upload/InfoBanner/zyjiaocheng/744/e6db084f9894412091ae78e8caa9184b.jpg)
我是JAVA编程的新手,之前只用Python编程.
我正在试图找出为什么当我执行我的代码时,我会在墙上找到一堆重复的“啤酒瓶#”.
package BeerBottle;
public class BeerBot {
public static void main (String [] args){
int beerNum = 99;
String word = "bottles";
while (beerNum > 0) {
if (beerNum == 1) {
word = "bottle";
} else {
word = "bottles";
}
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"); // I think it might be this line but I need it
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
我得到的结果是这样的:
2 bottles of beer on the wall
2 bottles of beer on the wall (duplicate)
2 bottles of beer
Take one down
pass it around
1 bottles of beer on the wall
1 bottle of beer on the wall (duplicate)
1 bottle of beer
Take one down
pass it around
No more bottles of beer on the wall
谢谢您的帮助
解决方法:
循环的最后一行打印行与下一循环的第一行打印行相同,所以这不是问题.要在视觉上分隔每个循环的输出,请在方法的最后打印一个空行.然后你会看到这样的事情:
2 bottles of beer on the wall // Last line of a loop
2 bottles of beer on the wall // First line of the next loop
2 bottles of beer
Take one down
pass it around
1 bottles of beer on the wall // Last line of a loop
1 bottle of beer on the wall // First line of next loop
1 bottle of beer
Take one down
pass it around
No more bottles of beer on the wall
内容总结
以上是互联网集市为您收集整理的试着学习JAVA,我想写99瓶啤酒歌全部内容,希望文章能够帮你解决试着学习JAVA,我想写99瓶啤酒歌所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。