-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
39 lines (33 loc) · 797 Bytes
/
FizzBuzz.java
File metadata and controls
39 lines (33 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class FizzBuzz{
//Call fizzBuzz on 1 - 100
public static void runFizzBuzz(){
for (int i = 1; i <= 100; i++){
fizzBuzz(i);
}
}
public static void fizzBuzz(int n){
//test if divisible by 3
boolean test3 = n % 3 == 0;
//test if divisible by 5
boolean test5 = n % 5 == 0;
if (test3 == true) {
if (test5 == true) {
//divisible by both 3 and 5
System.out.println("FizzBuzz");
} else {
//only divisible by 3
System.out.println("Fizz");
}
}
if (test5 == true) {
//only divisible by 5
System.out.println("Buzz");
} else {
//divisibly by neither
System.out.println(n);
}
}
public static void main(String[] args){
runFizzBuzz();
}
}