class FizzBuzz{
public static void main(String[] args){
for(int i = 1; i <= 100; ++i){
if(i % 3 == 0 && i % 5 == 0){
System.out.println("FizzBuzz");
}else if(i % 3 == 0){
System.out.println("Fizz");
}else if(i % 5 == 0){
System.out.println("Buzz");
}else{
System.out.println(i);
}
}
}
}
Currently, the shortest Java source code for FizzBuzz seems to be 96byte . Let's shorten it because it's a big deal (what?).
class FizzBuzz{
public static void main(String[] args){
for(int i = 1; i <= 100; ++i){
System.out.println(((i % 3 == 0 && i % 5 == 0) ? "FizzBuzz" : ((i % 5 == 0) ? "Buzz" : ((i % 3 == 0) ? "Fizz" : i + ""))));
}
}
}
The code will be convenient and easy to read if you do not make a mistake in using it. (Although I have different tastes) However, the above usage is overkill by any means, so do not do it unless you have a specific reason.
This is a matter of pure idea or not. Daily training and sense are important. I think you should read a lot of good code and write a lot of code yourself.
class FizzBuzz{
public static void main(String[] args){
for(int i = 1; i <= 100; ++i){
System.out.println((i % 3 > 0 ? "" : "Fizz") + (i % 5 > 0 ? (i % 3 > 0 ? i + "" : "") : "Buzz"));
}
}
}
class FizzBuzz{
public static void main(String[] args){
for(int i = 1; i <= 100; ++i){
System.out.println((i % 3 > 0 ? "" : "Fizz") + (i % 5 > 0 ? (i % 3 > 0 ? i : "") : "Buzz"));
}
}
}
--Until JLS 2nd Edition, the second and third expressions of the conditional operator had to return the same type.
――So, I forcibly make it a String with a power technique such as ʻi + "" . ――Ah, I didn't use
String # valueOf` because I wrote it for short coding purposes.
--The automatic type conversion rules have changed in JLS 3rd Edition so that i can now be passed without explicitly converting it to a String.
--Relationship with auto boxing and generics introduced?
class FizzBuzz{
public static void main(String[] args){
for(int i = 1; i <= 100; ++i)
System.out.println((i % 3 > 0 ? "" : "Fizz") + (i % 5 > 0 ? i % 3 > 0 ? i : "" : "Buzz"));
}
}
--If you understand the priority order of operators, you can reduce unnecessary parentheses. ――However, I think it is better to put parentheses appropriately so that the processing breaks can be clearly seen even if it becomes a little redundant so as not to impair readability. --The for block can also be deleted if there is only one sentence ――In addition, I personally usually do not omit parentheses even in one sentence for if and for blocks.
class FizzBuzz{
public static void main(String[] args){
for(int i = 0; ++i < 101;)
System.out.println((i % 3 > 0 ? "" : "Fizz") + (i % 5 > 0 ? i % 3 > 0 ? i : "" : "Buzz"));
}
}
--In Java, the language specification guarantees the evaluation order of the increment operator, so if you use the pre-increment operator, it will always be incremented before ** <101
is evaluated.
--Along with that, the start is changed from 1 to 0
--It has nothing to do with increment, but by the way, <= 100
is changed to <101
to reduce it by 1b.
class F{public static void main(String[]a){for(int i=0;++i<101;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
Remove extra whitespace and line breaks to make variables a single character. Decent? This 130 bytes is the limit in the source code. Perhaps.
I have an array declaration thanks to short coding
String[]a
I noticed that I can write by sticking together like this. Also
String
[]
a
It doesn't matter if you disassemble it like this.
Well, I don't choose and use arrays on my own nowadays ...
At the beginning, I said that the shortest is 96 bytes, but in the previous code it is 130 bytes. In fact, it is still shorter with special conditions.
It seems that VMs up to a certain version have a problem, and it was possible to output characters without the main method. (With the current VM, nothing should work without the main method.)
class F{static{for(int i=0;++i<101;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
It became 102 bytes. It seems that VMs up to a certain version will be executed in the static initializer earlier than the timing specified in the original VM specifications, and even such code will be executed. For this code, after FizzBuzz is run, I get angry that there is no main method.
By the way, a static initializer is a block of code declared in static that is executed only once when a class is loaded. Please google how to use it.
enum F{A;{for(int i=0;++i<101;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
It became 97 bytes.
enum
For Java enums, it's best to read Effective Java 2nd Edition. no doubt. As for the above code, it uses the following properties.
--enum is also a type of class --Fields, methods, and initializers can be defined only for classes. --The enumeration is an object of the enum and is instantiated when the enum is loaded
An instance initializer is a block of code that runs only once when an instance is created. Called before the constructor. Please google how to use it. I don't think there are many opportunities to use it.
enum F{A;int i;{for(;++i<101;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
The last 1 byte was reduced by using the property that the field has a default value (int is 0) so that you don't have to explicitly assign 0.
By short coding with FizzBuzz
--Conditional operator --Operator precedence --Increment operator --Static initializer --Instance initializer
I was able to learn the specifications of. Even though it's FizzBuzz, you can't underestimate it! !!
Recommended Posts