0% found this document useful (0 votes)
2 views1 page

Java FizzBuzz Program Example

This Java program takes a user-inputted number and prints either "Fizz", "Buzz", "FizzBuzz", or the number itself depending on whether the number is divisible by 3, 5, both, or neither. It implements the FizzBuzz coding challenge of printing strings for multiples of 3 and 5 and the number for non-multiples.

Uploaded by

dd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Java FizzBuzz Program Example

This Java program takes a user-inputted number and prints either "Fizz", "Buzz", "FizzBuzz", or the number itself depending on whether the number is divisible by 3, 5, both, or neither. It implements the FizzBuzz coding challenge of printing strings for multiples of 3 and 5 and the number for non-multiples.

Uploaded by

dd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 import [Link].

Scanner;
2
3 public class FizzBuzz {
4
5     public static void main(String[] args) {
6 answer();
7     }
8
9     public static void answer() {
10 Scanner input = new Scanner([Link]);  // Create a Scanner 
object
11
12 [Link]("Number: ");
13 int num = [Link]();
14
15 if(num % 5 == 0 && num % 3 == 0){
16 [Link]("FizzBuzz");
17 } else if(num % 5 == 0){
18 [Link]("Fizz");
19 } else if(num % 3 == 0){
20 [Link]("Buzz");
21 } else {
22 [Link](num);
23 }
24
25 // OR (more confusing)
26 if(num % 5 == 0){
27 if(num % 3 == 0){
28 [Link]("FizzBuzz");
29 } else {
30 [Link]("Fizz");
31 }
32 } else if(num % 3 == 0){
33 [Link]("Buzz");
34 } else {
35 [Link](num);
36 }
37     }
38
39     public static void practice(){
40
41     }
42
43 }
44

Page 1 of 1

You might also like