-
Notifications
You must be signed in to change notification settings - Fork 0
basic fizzbuzz using java ArrayList #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,25 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class FizzBuzz { | ||
| public static void main(String[] args) throws Exception { | ||
|
|
||
| ArrayList<String> result = new ArrayList<String>(); | ||
|
|
||
| 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); | ||
| if (i % 3 == 0) { | ||
| result.add("Fizz"); | ||
| } | ||
| if (i % 5 == 0) { | ||
| result.add("Buzz"); | ||
| } | ||
| if (result.size() == 0) { | ||
| result.add(String.valueOf(i)); | ||
| } | ||
|
Comment on lines
+16
to
+18
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also a scenario in which I can introduce my favourite programming operator called the "ternary operator" https://www.w3schools.com/java/java_conditions_shorthand.asp its perfect for when you want to do "if then else ". In this case it could be within the |
||
|
|
||
| System.out.println(String.join("", result)); | ||
|
|
||
| result.clear(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice this is a pretty good approach! For performance and memory reasons though, you might want to use a "StringBuilder" https://www.geeksforgeeks.org/stringbuilder-class-in-java-with-examples/
Since you're never doing any special array functions (like sorting, splicing etc)