i\'m in java 1 and i need help with this program. please help me. thank you 1. f
ID: 3671252 • Letter: I
Question
i'm in java 1 and i need help with this program. please help me. thank you
1. forloops
Exercise Location
For this exercise, you will copy a program from my directory into yours. It doesn't matter what directory this exercise is located in. You will need to copy this command and paste it into Loki:
cp ~lkoperski/1404_Lab/Exercises/Exercise06v2.java .
Exercise Description
Using a for loop, you must output the song lyrics to "99 bottles of beer." We will start at 9 instead of 99, and use "special beverages" instead of "beer."
Your program should look exactly like the output below.
9 bottles of special beverages on the wall, 9 bottles of special beverages! Take one down, pass it around, 8 bottles of special beverages on the wall!
8 bottles of special beverages on the wall, 8 bottles of special beverages! Take one down, pass it around, 7 bottles of special beverages on the wall!
7 bottles of special beverages on the wall, 7 bottles of special beverages! Take one down, pass it around, 6 bottles of special beverages on the wall!
6 bottles of special beverages on the wall, 6 bottles of special beverages! Take one down, pass it around, 5 bottles of special beverages on the wall!
5 bottles of special beverages on the wall, 5 bottles of special beverages! Take one down, pass it around, 4 bottles of special beverages on the wall!
4 bottles of special beverages on the wall, 4 bottles of special beverages! Take one down, pass it around, 3 bottles of special beverages on the wall!
3 bottles of special beverages on the wall, 3 bottles of special beverages! Take one down, pass it around, 2 bottles of special beverages on the wall!
2 bottles of special beverages on the wall, 2 bottles of special beverages! Take one down, pass it around, 1 bottles of special beverages on the wall!
1 bottles of special beverages on the wall, 1 bottles of special beverages! Take one down, pass it around, 0 bottles of special beverages on the wall!
No more bottles of special beverages on the wall, no more bottles of special beverages!
Go to the store, buy some more, 99 bottles of special beverages on the wall! user@loki:~$
Explanation / Answer
public class HelloWorld {
public static void main(String[] args) {
for( int i = 9; i >= 0; i-- )
{
//If current bottle count after taking one down is 1, bottle will be used instead of bottles
if( i == 2 )
{
System.out.println( i + " bottles of special beverages on the wall, " + i + " bottles of special beverages.");
System.out.println( "Take one down and pass it around, " + (i - 1) + " bottle of special beverages on the wall. ");
}
//If current bottle count is 1, bottle will be used instead of bottles
//If current bottle count after taking one down is 0, no more bottles should be printed
else if( i == 1)
{
System.out.println( i + " bottle of special beverages on the wall, " + i + " bottle of special beverages.");
System.out.println( "Take one down and pass it around, no more bottles of special beverages on the wall. ");
}
//If current bottle count is 0, no more bottles should be printed
//If there are no more bottles, go to the store and get more bottles
else if ( i == 0 )
{
System.out.println( "No more bottles of special beverages on the wall, no more bottles of special beverages.");
System.out.println( "Go to the store and buy some more, 99 bottles of special beverages on the wall.");
}
//Current count of bottles
else
{
System.out.println( i + " bottles of special beverages on the wall, " + i + " bottles of special beverages.");
System.out.println( "Take one down and pass it around, " + (i - 1) + " bottles of special beverages on the wall. ");
}
}
}
}
output
9 bottles of special beverages on the wall, 9 bottles of special beverages.
Take one down and pass it around, 8 bottles of special beverages on the wall.
8 bottles of special beverages on the wall, 8 bottles of special beverages.
Take one down and pass it around, 7 bottles of special beverages on the wall.
7 bottles of special beverages on the wall, 7 bottles of special beverages.
Take one down and pass it around, 6 bottles of special beverages on the wall.
6 bottles of special beverages on the wall, 6 bottles of special beverages.
Take one down and pass it around, 5 bottles of special beverages on the wall.
5 bottles of special beverages on the wall, 5 bottles of special beverages.
Take one down and pass it around, 4 bottles of special beverages on the wall.
4 bottles of special beverages on the wall, 4 bottles of special beverages.
Take one down and pass it around, 3 bottles of special beverages on the wall.
3 bottles of special beverages on the wall, 3 bottles of special beverages.
Take one down and pass it around, 2 bottles of special beverages on the wall.
2 bottles of special beverages on the wall, 2 bottles of special beverages.
Take one down and pass it around, 1 bottle of special beverages on the wall.
1 bottle of special beverages on the wall, 1 bottle of special beverages.
Take one down and pass it around, no more bottles of special beverages on the wall.
No more bottles of special beverages on the wall, no more bottles of special beverages.
Go to the store and buy some more, 99 bottles of special beverages on the wall.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.