Results 1 to 2 of 2
  1. #1
    Logic32 is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Help needed with Green Bottles activity

    Hello all,
    for my computing class I need to write up a code that will print the lyrics of the Green Bottles song. So it will ask the user to input a number and it will use that number to print the lyrics and then count down from that number. For example, user enters the number 5, the program will run and display:
    "5 green bottles, hanging on the wall
    5 green bottles hanging on the wall
    and if one green bottles should fall
    there will be 4 green bottles hanging on the wall

    4 green bottles, hanging on the wall...." and so on.

    So far I have managed to write a code that will display the number that is entered but I cant figure out how to subtract the number by 1 to continue it. My code so far:
    import java.util.Scanner;

    public class SomeJavaCode
    {
    public static void main(String[] args)
    {

    System.out.println("How many bottles to start with?");

    Scanner botnum = new Scanner(System.in);

    int i = botnum.nextInt();

    if (i == 4)

    {

    System.out.println(botnum + "green bottles, hanging on the wall");

    System.out.println(botnum + "green bottles hanging on the wall");

    System.out.println(botnum + "and if one green bottle should fall");

    System.out.println(botnum + "there will be" "green bottles hanging on the wall");

    }

    } // end of main method
    } // end of class
    Please could someone help me code it so that it substracts the number and repeats the rhyme till 0. Thank you.

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    181
    Rep Power
    0

    Default Re: Help needed with Green Bottles activity

    Have you learned about forloops yet?

    If you haven't, here is some brief info -

    Java Code:
    for(initialize; check; change) {
    do this;
    }
    That is the basic format. I'll show some examples - it is easier to understand from examples.

    Java Code:
    int max = 6;
    for(int i = 0; i < 6; i++) {
    System.out.println("The number: " + i);
    }
    This will print out:
    This number: 0
    This number: 1
    This number: 2
    This number: 3
    This number: 4
    This number: 5
    Use that. If the example isn't enough for you to understand, simply google "java forloops" and read some stuff.

Similar Threads

  1. 100 Bottles of Beer issue
    By PPhillips in forum New To Java
    Replies: 1
    Last Post: 05-03-2013, 07:46 PM
  2. 10 green bottles
    By Dave013 in forum New To Java
    Replies: 2
    Last Post: 10-25-2011, 06:41 PM
  3. Replies: 0
    Last Post: 10-01-2011, 04:14 AM
  4. Replies: 1
    Last Post: 09-12-2011, 02:09 AM
  5. 'Bottles of Beer' project
    By s4rd59 in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •