Assignment Brief:
Write an application using an integer and string variable, a while loop and an if test/statement to
output the following song on the screen:
99 bottles of beer on the wall
99 bottles of beer
Take one down
Pass it around

“Space” ….. blank line between verses

98 bottles of beer on the wall …….
Download full brief here


Download source code for 'BottlesOnTheWall' here

// Start of code---------------------
import java.util.Scanner;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */

public class BottlesOnAWall {

/**
 * This program is designed to take in a numeric digit/digits from a user
 * A valid input is stored and used as a starting point for The bottles on the wall song
 *
 */

    public static void main(String[] args) {

        final String lineStart = " bottles of beer on the wall\n";
        final String lineMiddle = " bottles of beer\n";
        final String lineEnd = "Take one down\nPass it around\n";
        final String counterError = "\nInvalid entry!!\nPlease input 0-9 digits only to continue: ";

        Scanner input = new Scanner(System.in);

        System.out.println("How may bottles would you like to start with?");

        //Input from the user must be a numeric digit
        while (!input.hasNextInt()) {
            System.out.println(counterError);
            input.next();
        }
        int counter = input.nextInt();
        System.out.println();
      
                //Close Scanner input
        input.close();
      
        //Code for song verse
        for (int i = counter; i >= 0; i--) {
            if (i > 1) {//Plural bottle verse
                System.out.println(i + lineStart + i + lineMiddle + lineEnd);
            }
            else if (i == 1) {//Singular bottle verse
                System.out.println(i + " bottle of beer on the wall\n" + i
                        + " bottle of beer\n" + lineEnd);
            }
            else if (i == 0) {//Final verse - end line
                System.out.println("No more" + lineStart);
            }
        }
    }
  
}//End of code---------------------

0 comments:

Post a Comment

Please feel free to leave comments or ask questions related to the tutorials.