0% found this document useful (0 votes)
23 views

Create A Scanner Object: Import Public Class Public Static Void

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)
23 views

Create A Scanner Object: Import Public Class Public Static Void

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
You are on page 1/ 1

1 import java.util.

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(System.in);  // Create a Scanner 
object
11
12 System.out.print("Number: ");
13 int num = input.nextInt();
14
15 if(num % 5 == 0 && num % 3 == 0){
16 System.out.println("FizzBuzz");
17 } else if(num % 5 == 0){
18 System.out.println("Fizz");
19 } else if(num % 3 == 0){
20 System.out.println("Buzz");
21 } else {
22 System.out.println(num);
23 }
24
25 // OR (more confusing)
26 if(num % 5 == 0){
27 if(num % 3 == 0){
28 System.out.println("FizzBuzz");
29 } else {
30 System.out.println("Fizz");
31 }
32 } else if(num % 3 == 0){
33 System.out.println("Buzz");
34 } else {
35 System.out.println(num);
36 }
37     }
38
39     public static void practice(){
40
41     }
42
43 }
44

Page 1 of 1

You might also like