Try to solve a restricted FizzBuzz problem in Java

what will you do

I created a Qiita account, but I didn't write anything The FizzBuzz problem, which has been talked about to the point of practicing article posting, is now available in Java. Try to solve it. If you solve it normally as it is, it will be really meaningless, so This time I made it according to the following rules.

No conditional branch

String[] fizz = new String[]{"Fizz", " ", " "}; //There is a half-width space in indexes 1 and 2.
String[] buzz = new String[]{"Buzz", " ", " ", " ", " "}; //There is a half-width space in indexes 1 to 4.

for (int i = 1; i <= 100; i++) {
    System.out.println((fizz[i % 3] + buzz[i % 5]).replaceAll("^\\s+$", String.valueOf(i)).trim());
}

If you write a process that says 0 when it is divisible by 3, 5, 15 and 1 otherwise, it will be refreshing where there is a blank character string. I couldn't think of ...

No division and surplus

int fizzCounter = 0;
int buzzCounter = 0;

for (int i = 1; i <= 100; i++) {
    fizzCounter++;
    buzzCounter++;
    if (fizzCounter != 3 && buzzCounter != 5) {
        System.out.print(i);
    }
    if (fizzCounter == 3) {
        System.out.print("Fizz");
        fizzCounter = 0;
    }
    if (buzzCounter == 5) {
        System.out.print("Buzz");
        buzzCounter = 0;
    }
    System.out.println();
}

It's faster to look at the last digit in 15-ary, but I don't understand such a difficult story. </ S>

No repetitive sentences

public static void main(String[] args) {
    printFizzBuzz(1);
}

static void printFizzBuzz(int num) {
    if (num % 15 == 0) {
        System.out.println("FizzBuzz");
    } else if (num % 3 == 0) {
        System.out.println("Fizz");
    } else if (num % 5 == 0) {
        System.out.println("Buzz");
    } else {
        System.out.println(num);
    }

    if (num < 100) {
        printFizzBuzz(num + 1);
    } else {
        return;
    }
}

No matter how much you talk about it and how efficient and smart it is to write I think it's really important to actually transcribe and move it with the logic you can think of.

Recommended Posts

Try to solve a restricted FizzBuzz problem in Java
Try to solve Project Euler in Java
Try to create a bulletin board in Java
How to solve an Expression Problem in Java
[Java] Try to solve the Fizz Buzz problem
Try to implement Yubaba in Java
Interface Try to make Java problem TypeScript 7-3
[Java] Try to solve the Fizz Buzz problem using recursive processing
Try to implement n-ary addition in Java
Try making a calculator app in Java
FizzBuzz in Java
Two ways to start a thread in Java + @
How to display a web page in Java
Code to escape a JSON string in Java
Increment behavior Try to make Java problem TypeScript 3-4
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
String operation Try to make Java problem TypeScript 9-3
I made a simple calculation problem game in Java
How to create a Java environment in just 3 seconds
[Java] I tried to solve Paiza's B rank problem
Initialization of for Try to make Java problem TypeScript 5-4
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
I tried to make a login function in Java
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Try to build a Java development environment using Docker
Try using RocksDB in Java
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Find a subset in Java
[Beginner] Try to make a simple RPG game with Java ①
Convert a Java byte array to a string in hexadecimal notation
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
Create a method to return the tax rate in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
I tried to make a client of RESAS-API in Java
Try implementing Android Hilt in Java
A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019
Solve AtCoder Beginner Contest 150 in java
Try to make a simple callback
Try implementing GraphQL server in Java
3 Implement a simple interpreter in Java
Try to implement Yubaba in Ruby
Multithreaded to fit in [Java] template
I created a PDF in Java.
Solve AtCoder Beginner Contest 153 in java
How to try Java preview features (such as Java 15 Record) in IntelliJ
How to implement a job that uses Java API in JobScheduler
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to learn JAVA in 7 days
How to automatically operate a screen created in Java on Windows
Try running Selenuim 3.141.59 in eclipse (java)
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
[Java] How to create a folder