Write a program that plays the old fashioned FizzBuzz game in Java that gets val
ID: 3887270 • Letter: W
Question
Write a program that plays the old fashioned FizzBuzz game in Java that gets values f and b from the user, and then prints out numbers substituting “Fizz”, “Buzz”’, and “FizzBuzz”. The user also gets to pick the numbers at which to start and end counting. The program quits when either f or b is zero. Global public static BufferedReader as the only BufferedReader object in the entire program for reading user input. Display error message when the user enters a Fizz or Buzz number that is negative and when the user enters an ending number that is smaller than the starting number.
Explanation / Answer
The Code is as follows:
import java.io.*;
public class Fizzbuzz{
public static void main(String[] args) throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter the Values for f and b:");
int f = 0;
int b = 0;
int ch = 1;
f = Integer.parseInt(input.readLine());
b = Integer.parseInt(input.readLine());
if( f == 0 || b == 0)
{
System.out.println("Please don't use zero") ;
System.out.print("Try Again");
}
else
{
System.out.println("list is");
for (int i = f; i < b+1; i++) {
// Set this to true when one of the special conditions is met.
boolean printed = false;
if (i % 3 == 0) {
// When i is divisible by 3, then print "Fizz"
printed = true;
System.out.print("Fizz");
} else if (i % 5 == 0) {
// When i is not divisible by 3 but is divisible by 5, then print "Buzz"
printed = true;
System.out.print("Buzz");
}
if (i % 15 == 0) {
printed = true;
System.out.print("FizzBuzz");
}
if (printed == false) {
// The number does not satisfy any of the special conditions above.
System.out.print(i);
}
System.out.println();
}
}
}
}
OUTPUT:
Enter the values for f and b:
1
10
list is:
if there is any query please ask in comments.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.