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