Skip to content

Instantly share code, notes, and snippets.

@jirrick
Last active July 31, 2017 17:52
Show Gist options
  • Save jirrick/bf66d866ea58a2df1402a6f4878d651a to your computer and use it in GitHub Desktop.
Save jirrick/bf66d866ea58a2df1402a6f4878d651a to your computer and use it in GitHub Desktop.
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