asked 87.4k views
4 votes
Assignment 1 is to write a program that will write the lyrics to "X number of beers on the wall". Use only the main method. Prompt for the initial number of bottles of beer on the wall. Confirm the value is greater than zero and less than 101. Your program should then generate the following console output. The example below assumes the number 3 has entered. You loop each time until there are 0 bottles of beer on the wall. 3 bottles of beer on the wall. 3 bottles of beer. Take 1 down, pass it around. 2 bottles of beer on the wall. 2 bottles of beer on the wall. 2 bottles of beer. Take 1 down, pass it around. 1 bottle of beer on the wall. 1 bottle of beer on the wall. 1 bottle of beer. Take 1 down, pass it around. 0 bottles of beer on the wall.

asked
User JeffK
by
8.0k points

1 Answer

7 votes

Answer:

// here is code in java.

import java.util.*;

class Main

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// variable to store the number of bottles

int num_bot;

Scanner scr=new Scanner(System.in);

System.out.print("Enter number of bottles of beer on the wall:");

//read the number of bottles

num_bot=scr.nextInt();

// print the output

for(int i=num_bot;i>0;i--)

{

// print the first statement

System.out.println(i+" beer bottles on the wall");

// print the second statement

System.out.println(i+" beer bottles . Take 1 down and pass the bottle around.");

}

// when there is 0 bottle

System.out.println("0 bottles of beer on the wall.");

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Declare a variable "num_bot" to store the initial number of beer bottles.Then ask user to give number of bottles.Run a for loop to pass a beer bottle until there is 0 bottle on the wall. When number of bottle is 0 then it will print there is "0 bottle on the wall".

Output:

Enter number of bottles of beer on the wall:3

3 beer bottles on the wall

3 beer bottles . Take 1 down and pass the bottle around.

2 beer bottles on the wall

2 beer bottles . Take 1 down and pass the bottle around.

1 beer bottles on the wall

1 beer bottles . Take 1 down and pass the bottle around.

0 bottles of beer on the wall.