A game where the first player says "1" and the next player says the next number of the previous player
However ...
--If it is divisible by 3, "Fizz" —— “Buzz” if divisible by 5 --"Fizz Buzz" if divisible by both (ie 15)
There is a rule
Since the FizzBuzz problem can be output with a relatively simple program, It's a barrier that novice programmers should overcome
for(int i = 1;i < 100;i++){
System.out.println(i);
}
--Declare an integer type (Integer) --Initialize with the subscript "i" as 1. --Increment (increment) i to 99 (not including 100), --Output the incremented subscript i with line breaks
1
2
3
(Omission)
97
98
99
Output up to 99
Concatenate the string "is Fizz"
for(int i = 1;i < 100;i++){
System.out.println(i + "Is Fizz");
}
1 is Fizz
2 is Fizz
3 is Fizz
4 is Fizz
5 is Fizz
(Omission)
15 is Fizz
(Omission)
96 is Fizz
97 is Fizz
98 is Fizz
99 is Fizz
1 and 3 and 15 have become Fizz This is because there is no conditional branching such as "if it is a multiple of 3", "if it is a multiple of 5", "if it is a multiple of 3 and 5", and "if it is not".
Add "Fizz" if it is a multiple of 3, otherwise output a number
for(int i = 1;i < 100;i++){
if(i % 3 == 0){
System.out.println(i + "Is Fizz");
}
else{
System.out.println(i);
}
}
"I% 3 == 0" is The remainder of dividing i by 3 is 0 That means it is a multiple of 3
1
2
3 is Fizz
4
5
6 is Fizz
7
8
9 is Fizz
10
(Omission)
15 is Fizz
16
17
18 is Fizz
(Omission)
93 is Fizz
94
95
96 is Fizz
97
98
99 is Fizz
Add "Fizz" if it is a multiple of 3, "Buzz" if it is a multiple of 5, otherwise output a number.
for(int i = 1;i < 100;i++){
if(i % 3 == 0){
System.out.println(i + "Is Fizz");
}
else if(i % 5 == 0){
System.out.println(i + "Is Buzz");
}
else{
System.out.println(i);
}
}
1
2
3 is Fizz
4
5 is Buzz
6 is Fizz
7
8
9 is Fizz
10 is Buzz
11
12 is Fizz
13
14
15 is Fizz
16
15 is Fizz! This is because the upper process satisfies the condition and the lower process is skipped. For this reason, it is necessary to write in order from the "narrower" condition.
if(i % 3 == 0){
System.out.println(i + "Is Fizz"); //The condition is met here and the processing below is skipped
}
else if(i % 5 == 0){
System.out.println(i + "Is Buzz");
Add "Fizz" if it is a multiple of 3, "Buzz" if it is a multiple of 5, otherwise output a number. Connect conditional expressions with "&&" to satisfy multiples of 5 at the same time as multiples of 3
i % 3 == 0 && i % 5 == 0
for(int i = 1;i < 100;i++){
if(i % 3 == 0 && i % 5 == 0){
System.out.println(i + "Is Fizz Buzz");
}
else if(i % 3 == 0){
System.out.println(i + "Is Fizz");
}
else if(i % 5 == 0){
System.out.println(i + "Is Buzz");
}
else{
System.out.println(i);
}
}
1
2
3 is Fizz
4
5 is Buzz
6 is Fizz
7
8
9 is Fizz
10 is Buzz
11
12 is Fizz
13
14
15 is Fizz Buzz
15 is properly output as Fizz Buzz No!
If you write from a wide range of conditions ...
for(int i = 1;i < 100;i++){
if(i % 3 == 0){
System.out.println(i + "Is Fizz");
}
else if(i % 5 == 0){
System.out.println(i + "Is Buzz");
}
else if(i % 3 == 0 && i % 5 == 0){
System.out.println(i + "Is Fizz Buzz");
}
else{
System.out.println(i);
}
}
1
2
3 is Fizz
4
5 is Buzz
6 is Fizz
7
8
9 is Fizz
10 is Buzz
11
12 is Fizz
13
14
15 is Fizz
After all "Fizz" is prioritized and it will be strange, so be careful
Recommended Posts