[GO] [Java] Try to solve the Fizz Buzz problem

Introduction

Since I have completely lost the opportunity to touch Java at work, I thought about how to solve the "Fizz Buzz problem" in various ways using Java.

What is Fizz Buzz?

Fizz Buzz - Wikipedia

General method using For statement

FizzBuzz.java


	/**
	 *A common method using a for statement.
	 * @param end The number to end FizzBuzz.
	 */
	public static void useForLoop(int end) {
		for (int i=1; i<=end; i++) {
			if (i%3==0 && i%5==0) {
				System.out.println("Fizz Buzz");
			} else if (i%3==0) {
				System.out.println("Fizz");
			} else if (i%5==0) {
				System.out.println("Buzz");
			} else {
				System.out.println(i);
			}
		}
	}

Method using For statement + ternary operator

FizzBuzz.java


	/**
	 *A method that utilizes the ternary operator.
	 * @param end The number to end FizzBuzz.
	 */
	public static void useForLoopWithTertiaryOperator(int end) {
		for (int i=1; i<=end; i++) {
			System.out.println(
					(i%3==0 && i%5==0) ? "Fizz Buzz" : (i%3==0) ? "Fizz" : (i%5==0) ? "Buzz" : i
					);
		}
	}

Method using Stream API

FizzBuzz.java


import java.util.stream.IntStream;
...

	/**
	 *A method that utilizes the Stream API.
	 * @param end The number to end FizzBuzz.
	 */
	public static void useStreamAPI(int end) {
		IntStream.rangeClosed(1, end)
			.mapToObj(i -> (i%3==0 && i%5==0) ? "Fizz Buzz" : (i%3==0) ? "Fizz" : (i%5==0) ? "Buzz" : i)
			.forEach(msg -> System.out.println(msg));
	}

Summary

Recommended Posts

[Java] Try to solve the Fizz Buzz problem
Try to solve a restricted FizzBuzz problem in Java
Interface Try to make Java problem TypeScript 7-3
Try to solve Project Euler in Java
Increment behavior Try to make Java problem TypeScript 3-4
String operation Try to make Java problem TypeScript 9-3
How to solve an Expression Problem in Java
[Java] I tried to solve Paiza's B rank problem
Let's solve the FizzBuzz problem!
Input to the Java console
You can solve the problem by referring to the two articles !!!
[java8] To understand the Stream API
Welcome to the Java Library Swamp! !!
[Java] Try to implement using generics
Try to extract java public method
Try to implement Yubaba in Java
The road from JavaScript to Java
I tried to solve the problem of "multi-stage selection" with Ruby
How to solve the unknown error when using slf4j in Java
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to solve the problem of Google Tech Dev Guide
[Java] How to use the File class
Introduction to java for the first time # 2
[Java] How to use the hasNext function
Try to implement n-ary addition in Java
Try using the Stream API in Java
Java SE8 Silver ~ The Road to Pass ~
[Java] How to use the HashMap class
How to solve the problem that notification cannot be requested on iOS14
About the procedure for java to work
[Java] How to use the toString () method
Java implementation to create and solve mazes
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
Try calling the CORBA service in Java 11+
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
[Processing × Java] How to use the class
How to install the legacy version [Java]
How to get the date in java
Output of the book "Introduction to Java"
Try changing the .erb file to .slim
[Processing × Java] How to use the function
I went to the Java Women's Club # 1
[Java] Color the standard output to the terminal
[Java] How to use the Calendar class
Try using the Wii remote with Java
Try adding text to an image in Scala using the Java standard library
Try calling Watson NLU that seems to support Japanese from the Java SDK
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
Try to access the on-premise system from SAP Cloud Platform --Java App Edition
How to solve the font specification problem dedicated to using IntelliJ IDEA (Win x64)
Try to build Java8 environment on Amazon Linux2
[Java] How to use Thread.sleep to pause the program
Try to link Ruby and Java with Dapr
Try calling the CORBA service from Spring (Java)
[Competition Pro] Solve the knapsack problem with Ruby
Try accessing the dataset from Java using JZOS
[Java] (for MacOS) How to set the classpath
Try the free version of Progate [Java II]