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
为什么String字不等于“瓶子”?相反,它在“拿一个,将它传递给1瓶啤酒。”中说“瓶子”。
在“墙上的一瓶啤酒,一瓶啤酒”之后,它没有说“带一个下来通过它”
答案 0 :(得分: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");
}
}
}
}
它将与墙上的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("************")
因为它会给出一个清晰的想法
当一个循环结束而其他循环开始时。
答案 1 :(得分:1)
试试这个会起作用..
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");
}
else {
System.out.println("no more bottles of beer on the wall");
}
}
}
答案 2 :(得分:0)
在你减去瓶号而不是之前
之后,只需把你写的这段代码if (beerNum == 1) {
word = "bottle"; //ONE bottle
}
所以你的代码将是
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");
}
}
}
答案 3 :(得分:0)
使用for
循环播放瓶子歌曲的简单解决方案如下:
public class apples {
public static void main(String[] args) {
for (int i = 99; i >= 0; i--) {
if (i > 1) {
System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer.");
System.out.println("Take one down and pass it around, " + (i - 1) + " bottles of beer on the wall.");
} else if (i == 1) {
System.out.println(i + " bottle of beer on the wall, " + i + " bottle of beer.");
System.out.println("Take one down and pass it around, " + (i - 1) + " bottle of beer on the wall.");
} else if (i == 0) {
System.out.println("No more bottles of beer on the wall, no more bottles of beer.");
System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
}
}
}
}
答案 4 :(得分:0)
不,不,不。这绝对不像它应该的那么神秘。用我的解决方案去吧;)
public class Main {
static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer"; }
public static void main(String [] arg) {
String [] btls = { " on the wall, ", "\nTake one down, pass it round, " };
for(int i=99;i>0;i--) System.out.println("" + i + btl(i) + btls[0] + i + btl(i) + btls[1] + ((i>1)?i+btl(i):"no more bottles of beer"));
}
}
它就像一个魅力;)
....
4 bottles of beer on the wall, 4 bottles of beer
Take one down, pass it round, 4 bottles of beer
3 bottles of beer on the wall, 3 bottles of beer
Take one down, pass it round, 3 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer
Take one down, pass it round, 2 bottles of beer
1 bottle of beer on the wall, 1 bottle of beer
Take one down, pass it round, no more bottles of beer
而且,您也可以随时使用递归调用;)
public class Main {
static String [] btls = { " on the wall, ", "\nTake one down, pass it round, " };
static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer";}
static int drink(int i) {
System.out.println(""+i+btl(i)+btls[0]+i+btl(i)+btls[1]+((i>1)?i+btl(i):"no more bottles of beer"));
return (i>1)?drink(i-1):0;
}
public static void main(String [] arg) { drink(99); }
}
答案 5 :(得分:0)
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>1)
{
System.out.println(beerNum+" "+word+" of beer on the wall");
}
if(beerNum==1)
{
word = "bottle";
System.out.println(beerNum+" "+word+" of beer on the wall");
}
else
{
System.out.println("NO more bottles of the beer on the wall");
}
}
}
}
答案 6 :(得分:0)
我也想出了这个解决方案 公共课程主要{
[assembly: OwinStartup(typeof(...MyStartup))]
namespace MyNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions {
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType
});
}
}
}
答案 7 :(得分:-1)
我的版本的BeerSong程序递归方法。
public class BearSong {
public static void main(String [] arg) {
beer(99);
}
public static void beer(int n) {
System.out.println(n + " bottles of beer on the wall");
System.out.println(n + " bottles of beer");
System.out.println("ya\' take one down, ya\' pass it around,");
lessBottle(n-1);
}
public static void lessBottle(int x ) {
System.out.println( x + " bottles of beer on the wall.");
System.out.println();
beerLess(x);
}
private static void beerLess(int x) {
if (x > 0) {
beer(x);
}else {
System.out.println("No bottles of beer on the wall");
System.out.println("no bottles of beer,");
System.out.println("ya’ can’t take one down, ya’ can’t pass it around,");
System.out.println("’cause there are no more bottles of beer on the wall!");
}
}
}