patternjavaModerate
FizzBuzz implementation using the ternary operator
Viewed 0 times
theoperatorfizzbuzzternaryusingimplementation
Problem
This is my first time writing JAVA, and I'm trying to start off on the right track.
Using my C++ knowledge, I was able to understand the syntax for the ternary operator, but I'm not sure how it should be approached properly in JAVA. I have used Ideone to test this, which include
I'd like a review over anything there is to offer, even if it involves telling me that starting with Ideone is not the best choice for whatever reasons.
Using my C++ knowledge, I was able to understand the syntax for the ternary operator, but I'm not sure how it should be approached properly in JAVA. I have used Ideone to test this, which include
importss that apparently aren't even needed for this program, or so it seems.I'd like a review over anything there is to offer, even if it involves telling me that starting with Ideone is not the best choice for whatever reasons.
import java.util.*;
import java.lang.*;
import java.io.*;
class BeginnerFizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; ++i) {
System.out.println(
(i % 15 == 0) ? "FizzBuzz" :
(i % 3 == 0) ? "Fizz" :
(i % 5 == 0) ? "Buzz" :
i
);
}
}
}Solution
even if it involves telling me that starting with Ideone is not the best choice for whatever reasons.
As a beginner, starting with Ideone is not the best choice IMO. For a number of reasons:
As for the code itself, it isn't that bad. It is good that you removed IdeOne's
Greg has already covered the aspects of the conditional operator, so there is only one thing I can possibly comment about, but this is just a personal opinion more than anything else and feel free to completely ignore this comment.
In Java, it is more common in such for-loops to use
When using
In Java, it is also exactly the same (the same applies for C#, if you're wondering).
Of course, using
As a beginner, starting with Ideone is not the best choice IMO. For a number of reasons:
- It is not a good practice to use
import xxx.xxx.*. Only import the things that you actually need. In this case, you don't even need to import anything (because the things that you need are part of thejava.langpackage.
- In IdeOne the filename is "Main.java" and there is no way to change that, which means that you will not learn that a public class in a file must have the same name as the file itself. I.e. the
public class SomeClassmust go in a file namedSomeClass.java.
- In IdeOne you are not able to declare a package name. A package name is mainly used to identify projects and the author of the code and so on. They are usually your domain-name backwards. For example, as I own
zomis.netmy package names arenet.zomis.someproject. If you don't own a domain, well... make one up for now.com.jamalcould work for you. Either way, usage of the "default"-package (i.e. not declaring a package at all) is discouraged.
- In IdeOne there is no direct feedback for compiler errors. You have to click "Run" on your code to make sure that it has no compiler errors. In all modern Java IDEs you automatically get such feedback whenever you have finished writing a line of code (sometimes you have to hit Ctrl+S to save though before it will show the errors properly). This may not feel important at first, but once you get used to it you will really miss the feature in other IDEs.
As for the code itself, it isn't that bad. It is good that you removed IdeOne's
throws Exception declaration.Greg has already covered the aspects of the conditional operator, so there is only one thing I can possibly comment about, but this is just a personal opinion more than anything else and feel free to completely ignore this comment.
for (int i = 1; i <= 100; ++i) {In Java, it is more common in such for-loops to use
i++ rather than ++i. Although this has no effect whatsoever in this case, and I'm sure you're aware of the differences between i++ and ++i from your C++ (or is it ++C?) experience.When using
++i or i++ alone (i.e. without an assignment to another variable), there is a myth that there is a performance difference in C++ and C, while apparently that is not the case.In Java, it is also exactly the same (the same applies for C#, if you're wondering).
Of course, using
i++ or ++i always matters when adding another assignment. If i = 1:a = i++will makea == 1
a = ++iwill makea == 2
i == 2after the statement in both cases
Code Snippets
for (int i = 1; i <= 100; ++i) {Context
StackExchange Code Review Q#56708, answer score: 15
Revisions (0)
No revisions yet.