Mac
Newbie
- Joined
- Jul 30, 2003
- Messages
- 778
- Reaction score
- 0
I'm a fairly new programmer, taking my first college level programming class this semester. Its in java.
one of the projects to do is to:
so far this is what i have, it is almost complete except a small problem.
for some reason, it gets to the "Again?(y/n): " but it doesnt allow the user to enter any input. it just quits. I dont get how it's exiting the main while loop. There must be something im missing, but i dont see it.
here's what the output looks like:
any help is appreciated. sorry for my noobness.
one of the projects to do is to:
Design and implement an application that prints the first few verses of the traveling song "One Hundred Bottles of Beer." Use a loop such that each iteration prints one verse. Read the number of verses to print from the user. Validate the input. The following is the first verse of the song:
100 bottles of beer on the wall
100 bottles of beer
If one of those bottles should happen to fall
99 bottles of beer on the wall.
At the end of each printing of the verses, prompt to determine if the users wants to do it again.
so far this is what i have, it is almost complete except a small problem.
Code:
//**************************************************
// BeerCount.java By: Matt LaDuca
//
// Inputs the number of lines of the "one hundred
// bottles of beer" song to be printed
//**************************************************
import java.util.Scanner;
public class BeerCount
{
public static void main(String[] args)
{
int numOfLines;
String another = "y";
Scanner scan = new Scanner(System.in);
while(another.equals("y"))
{
System.out.print("\nEnter the number of verses of the Beer song to be printed: ");
numOfLines = scan.nextInt();
while(numOfLines < 0 || numOfLines > 100)
{
System.out.print("Invalid input. Please enter a number between 0 and 100 (inclusive): ");
numOfLines = scan.nextInt();
}
for(int i = 100; i > 100 - numOfLines; i--)
{
System.out.println("\n" + i + " bottles of beer on the wall");
System.out.println("\n" + i + " bottles of beer");
System.out.println("\nIf one of those bottles should happen to fall");
System.out.println("\n" + (i - 1) + " bottles of beer on the wall");
}
System.out.print("Again? (y/n): ");
another = scan.nextLine();
}
}
}
for some reason, it gets to the "Again?(y/n): " but it doesnt allow the user to enter any input. it just quits. I dont get how it's exiting the main while loop. There must be something im missing, but i dont see it.
here's what the output looks like:
C:\Program Files\Java\LaDuca>java BeerCount
Enter the number of verses of the Beer song to be printed (0 to quit): 1
100 bottles of beer on the wall
100 bottles of beer
If one of those bottles should happen to fall
99 bottles of beer on the wall
Again? (y/n):
C:\Program Files\Java\LaDuca>
any help is appreciated. sorry for my noobness.