Last active
July 31, 2017 17:52
-
-
Save jirrick/bf66d866ea58a2df1402a6f4878d651a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class FizzBuzz | |
{ | |
Map<Integer, String> map; | |
public FizzBuzz() | |
{ | |
Init(); | |
} | |
private void Init() | |
{ | |
map = new HashMap<Integer, String>(); | |
map.put(3, "Fizz"); | |
map.put(5, "Buzz"); | |
map.put(7, "Wozz"); | |
// add more substitutions ... | |
} | |
public void Run() | |
{ | |
for(int i = 1; i <= 100; i++){ | |
System.out.print(Num2String(i)); | |
System.out.print("\n"); | |
} | |
} | |
private String Num2String(int i) | |
{ | |
String result = ""; | |
for (Map.Entry<Integer, String> pair : map.entrySet()){ | |
if (i % pair.getKey() == 0) result += pair.getValue(); | |
} | |
if (result.length() == 0) result = Integer.toString(i); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment