[英]if_else_loop program error the else statement does not display its content ( java code )
I am trying to display the 99 bottle beer song as per my code i am not getting the else statement, the program should have while and if statement, else statement though may or may not be included 我正在尝试按照我的代码显示99瓶啤酒的歌曲,但我没有得到else语句,程序应包含while和if语句,尽管可能包含或不包含else语句
public class bottles {
public static void main(String... s){
int beer = 99;
while(beer>=1){
if(beer>1){
System.out.print(beer + "bottles of beer on the wall ");
System.out.println(beer + "bottles of beer ");
System.out.print("Take one down and pass it around ");
beer = beer-1;
System.out.println(beer + "bottles of beer on the wall ");
}
if(beer==1){
System.out.print(beer + "bottles of beer on the wall ");
System.out.println(beer + "bottles of beer ");
System.out.print("Take one down and pass it around ");
System.out.println("no more bottles of beer on the wall. ");
System.out.println("");
}
else{
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 thewall");
}
}
}
}
output is not taking the else statement. 输出未使用else语句。 novice in java
Java新手
因为您的循环只会while(beer>=1)
The while
loop will execute until you have no more beer. 将执行
while
循环,直到您没有啤酒为止。 This means the last time it gets executed is with 1 beer left. 这意味着最后一次执行是剩下1杯啤酒。 Your
else
statement can never be executed. 您的
else
语句将永远无法执行。
So just put System.out.println("No more...
under your while
statement ( without the else
): 因此,只需将
System.out.println("No more...
放在您的while
语句下(不添加else
):
while( more than 0 beer) {
do sth...
}
// No more beers left
print "no more beers left";
Your else will never run ,As if beer>1 your first if works,if beer==1 ,it does beer-- ,so value of beer==0 ,and you are out of your loop.Beer value is ranging from 99 to 0. 您的别的将永远不会运行,就好像啤酒> 1如果您的第一个可行,如果啤酒== 1,它就做啤酒-,那么啤酒的值== 0,您就不在循环中了。啤酒的价值范围是99到0。
Conditions: 条件:
1: value>1 and <99 : First if value takes it and keeps decreasing 1:值> 1和<99:如果值取其值并持续减小,则首先
2: value reaches : 1 , 2nd if handles it for you , does beer-- ==0; 2:值达到:1,第二为您处理,啤酒-== 0;
3: value==0 , beer(0)>=1 is false so you are out of loop, while not executed . 3:value == 0,beer(0)> = 1为false,因此您处于循环状态,而未执行。 SO your else was never reached by code
所以你的其他人从来没有被代码到达
while(beer>=1) //will run only if beer>=1
{
if(beer>1)
{ //execute if beer>1
beer -- // this will work till beer>1
}
if(beer==1)
{ //if beer==1 , this will execute
beer-- // val of beer==0 ,So next time loop does't work
}
else{ // will be never called, if beer==0 you will never reach inside while so this never executes
}
}
}
Here's the code try this one 这是代码尝试这个
public static void main(String... args)
{
int beer = 10;
while(beer>=1){
if(beer>1){
System.out.print(beer + "bottles of beer on the wall ");
System.out.println(beer + "bottles of beer ");
System.out.print("Take one down and pass it around ");
beer = beer-1;
System.out.println(beer + "bottles of beer on the wall ");
}
if(beer==1){
System.out.print(beer + "bottles of beer on the wall ");
System.out.println(beer + "bottles of beer ");
System.out.print("Take one down and pass it around ");
System.out.println("no more bottles of beer on the wall. ");
System.out.println("");
}
else{
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 thewall");
}
beer--;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.