Home>

I wanted to make a language play for a performer in java, but it wasn't going very well so I asked a question.

public static void main (String [] args) {
    for (int i = 1;i<= 15;i ++) {
        if (i% 3 == 0 || Integer.toString (i) .contains ("3")) {
            System.out.println ("AHO");
        } else {
            System.out.println (Integer.toString (i));
        }
    }
  }
}
Code


・ It becomes "AHO" when it is a multiple of 3.
・ It becomes "One" when it is a multiple of 5.
・ It becomes "Ahowan" when the common multiple of 3 and 5.
* "13 is not a multiple of 3, so ignore"

I have made it so far by searching on the net and books, but I do not know where to write a multiple of 5 (i% 5 == 0) and how to write the code.
Also, "Ahowan", the last 15th line, does not know how to express the common multiples of 3 and 5. (At the same time)

Because I am a beginner, I will investigate small details. However, I would appreciate it if you could teach me as gently as possible.
I think there are various missing parts, but that's all.
Thank you.

You can easily run java on this site.

※ If the execution result is as follows, it is ok.
1
2
Aho
4
One
Aho
7
8
Aho
One
11
Aho
13
14
Ahowan

  • Answer # 1

    In just fizzbuzz?
    If you search for "fizzbuzz", you'll see a lot.

  • Answer # 2

    If Naveats, 13 is "Aho"?

    It's a common story, so you can find a lot of useful information if you search.
    Google Search-Java Naveats

  • Answer # 3

    There is a nostalgic FizzBuzz that I made when I started studying.

    public static void main (String [] args) {
            int start = 1;
            int end = 15;
            IntStream.rangeClosed (start, end)
                    .mapToObj (i->
                              (i% 15 == 0)? "FizzBuzz"
                            : (i% 3 == 0)? "Fizz"
                            : (i% 5 == 0)? "Buz"
                            : String.valueOf (i))
                    .forEach (System.out :: println);
        }

  • Answer # 4

    Is this it?
    The meaning of learning the algorithm now

  • Answer # 5

    By using Map, I tried to support numbers other than 3, 5 and more than 3 numbers.

    import java.util.HashMap;
    import java.util.Map;
    public class FizzBuzz {
        @SuppressWarnings ("serial")
        static Map<Integer, String>data = new HashMap<Integer, String>() {
            {
                put (3, "AHO");
                put (5, "One");
            }
        };
        public static void main (String [] args) {
            for (int i = 1;i<= 15;i ++) {
                System.out.println (fizzbuzz (i));
            }
        }
        static String fizzbuzz (int n) {
            String str = "";
            for (Map.Entry<Integer, String>entry: data.entrySet ()) {
                if (n% entry.getKey () == 0) {
                    str + = entry.getValue ();
                }
            }
            if (str.length () == 0) {
                str = "" + n;
            }
            return str;
        }
    }

    In the method code, 3, 5,"Aho" ;,"One"never appear.
    By simply changing the contents of the Map, you can change the number to be processed and the character string to be output.