99 Bottles of Beer or pop on the wall

I'm having an issue with this line of code Im trying to create a programs that sings 99 bottles of beer on the wall if the user inputs that there over the age of 17, and if there under the age of 17 then have the program sing 99 bottles of pop on the wall in a loop.

I'm taking a class in high school where I've primarily learn't python and not a different language like java and I feel like I'm way over my head and if you could help I'd really appreciate it.

import java.util.Scanner;

public class loops {

    public static void main(String[] args) {
            Scanner in = new Scanner (System.in);
            Scanner word = new Scanner(System.in);
            System.out.println("Before we start how old are you?");
            int age = in.nextInt();
            String s = ("A");
            if (age > 17) {
            System.out.println(" Would You Like Pop or Beer");
            }

            String a1 = word.next();
            String a2 = word.next();

            if (a1.equals("beer")) {


            for (int beers=99; beers > -1;) {

                System.out.print(beers + "bottle" +5+ "of beer on the wall, ");


                System.out.println(beers + "bottle" +5+ "Of beer on the wall ");

                if (beers ==0) {

                    System.out.print("Take one down pass it around,");

                    System.out.println("99 bottles of beer on the wall\n");

                    System.exit(0);
                }else {
                    System.out.print("Take one down, pass it around,");

                    a1 =(--beers== 1) ? "" : "a1";

                    System.out.println(beers + "bottle"+a1+ " of beer on the wall.\n");
                }}








            } else if (a1.equals("pop"));

    for (int pop =99; pop > -1;) {

            System.out.print(pop + "bottle" + a1

                    + "of pop on the wall,");

            System.out.println(pop + "bottle" + a1 + " of pop,");

            if (pop == 0) {

                System.out.println("Take one down pass it around,");

                System.out.println("99 bottles of pop on the wall\n" );

                System.exit(0);

            }else {
                System.out.print("Take one down pass it around,");

                a1 = (--pop ==1)?"" : "a1";     

            System.out.println(pop + "bottle" +5+ " of pop on the wall.\n");
            }}




 if  (age <= 17) {

    for (int pop = 99; pop > -1;) {

        int a21 =pop;

        System.out.print(pop + "bottle" + a21 + " of pop on the wall,");

        System.out.println(pop + "bottle" + a21 + "of pop,");

        if (pop==0) {

            System.out.print("go to the store buy some more,");

            System.out.println("99 bottles of pop on the wall");

            System.exit(0);

        }}}}}

1 answer

  • answered 2018-11-08 08:30 Sven

    You don't need all the different loops. Just one is enough.

    import java.util.Scanner;
    
    public class Loops {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Scanner word = new Scanner(System.in);
            System.out.println("Before we start how old are you?");
            int age = in.nextInt();
            String drinkType;
            if (age > 17) {
                System.out.println(" Would You Like Pop or Beer");
                drinkType = word.next();
    
            } else {
                drinkType = "Pop";
            }
    
            for (int drinks = 99; drinks >= 0;) {
                System.out.print(drinks + (drinks == 1 ? " bottle" : " bottles") + " of " + drinkType + " on the wall, ");
                System.out.println(drinks + (drinks == 1 ? " bottle" : " bottles") + " of " + drinkType + " on the wall ");
    
                System.out.print("Take one down pass it around, ");
                if (drinks-- == 0) {
                    System.out.println("99 bottles of " + drinkType + " " +
                                "on the wall");
                } else {
                    System.out.println(drinks + (drinks == 1 ? " bottle" : " bottles") + " of " + drinkType + " on the wall.");
                }
            }
        }
    }