-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT9.java
More file actions
31 lines (29 loc) · 760 Bytes
/
T9.java
File metadata and controls
31 lines (29 loc) · 760 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
import java.util.ArrayList;
import java.util.List;
public class T9 {
/*
* @param n: An integer
* @return: A list of strings.
*/
/**
* int n = 15;
T9 test = new T9();
System.out.println(test.fizzBuzz(n).toString());
*/
public List<String> fizzBuzz(int n) {
// write your code here
ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i%3 == 0 && i%5 == 0) {
list.add("fizz buzz");
} else if (i%3 == 0) {
list.add("fizz");
} else if (i%5 == 0) {
list.add("buzz");
} else {
list.add(i + "");
}
}
return list;
}
}