[GO] [Java] Things to be aware of when outputting FizzBuzz

Things to watch out for when outputting FizzBuzz

What is the FizzBuzz problem?

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

Write a for statement that will be a template

for(int i = 1;i < 100;i++){
    System.out.println(i);
  }

What does the above mean?

--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

Modify the above code

Concatenate the string "is Fizz"

for(int i = 1;i < 100;i++){
 System.out.println(i + "Is Fizz");
}	

result···###

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".

Output "Fizz" to multiples of 3

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

result###

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

Output "Buzz" to multiples of 3, "Fizz" and 5

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);
 }
}

result###

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

Oh! ??

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");

Multiples of 3 and multiples of 5

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

Complete!

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);
     }
}

result##

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!

By the way,

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);
        }
    }

result##

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

[Java] Things to be aware of when outputting FizzBuzz
Things to be aware of when writing Java
Things to be aware of when writing code in Java
[Java Silver] Things to be aware of regarding switch statements
To be aware of easy-to-read code
[Java] [Microsoft] Things to be aware of when including the JDBC driver for SQL Server in one jar
5 things new programmers should be aware of
[Java] Be aware of short circuits (short-circuit evaluation)
Java Servlet should be aware of multithreaded environment
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
Summarize the life cycle of Java objects to be aware of in Android development
Basic rules to be aware of to write easy-to-read code
[Rails] When using ajax, be aware of "CSRF measures".
I want to be aware of the contents of variables!
Be sure to compare the result of Java compareTo with 0
[Technical memo] Things to be careful of from an engineer's point of view when creating a view
Java to be involved from today
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
How to find the total number of pages when paging in Java
[java] Summary of how to handle char
Output of the book "Introduction to Java"
Precautions when migrating from VB6.0 to JAVA
[Java] When checking URL format with Bean Validation, it may be better not to use @URL of Hibernate Validator.
Summary of good points and precautions when converting Java Android application to Kotlin
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
Summary of points I was worried about when migrating from java to kotlin
[Java] [Spring] Record of failure to enable Hibernate filter when instantiating Spring Data Repository
Use jenv to enable multiple versions of Java
[java] Summary of how to handle character strings
[Java] Number of connections required when nesting transactions
[Java] Summary of how to abbreviate lambda expressions
CORBA seems to be removed in Java SE 11. .. ..
Verification of performance impact when using Java volatile
[Java] Be careful of the key type of Map
Summary of moss when updating from JMockit 1.4 to 1.30
There seems to be no else-if in java
What I did when I converted java to Kotlin
Things to keep in mind when committing to CRuby
[Java] char type can be cast to int type
Minecraft BE server development from PHP to Java
[Java] How to get the authority of the folder
[Introduction to Java] Basics of java arithmetic (for beginners)
Java Welcome to the Swamp of 2D Arrays
Things to watch out for in Java equals
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Introduction to Java] List of things that got caught by the 14th day of programming
Summary of how to use the proxy set in IE when connecting with Java