0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FizzBuzz問題

Posted at

FizzBuzz問題を解いてみました!
もっとすっきりかける気がしますがまた今度解いてみようと思います。

##FizzBuss問題
1以上100以下の整数を順に画面に出力します。

ただし、3の倍数の場合には”Fizz”、5の倍数の場合には”Buzz”、3かつ5の倍数の場合には”FizzBuzz”を、数字のかわりに画面に出力します。



public class FizzBuzz {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {  // forで1から100までの数でループ for (①初期化式; ②条件式; ④反復式)
            //反復式では繰り返し後に行いたい処理を書く(繰り返し時には必ずiが+1される)
            if (i % 3 == 0) {
                // 3の倍数かつ5の倍数のとき
                if (i % 5 == 0) {
                    System.out.println("FizzBuzz");
                    // 3の倍数のとき
                } else {
                    System.out.println("Fizz");
                }
            } else if (i % 5 == 0) {  // 5の倍数のとき
                System.out.println("Buzz");
                // どれにも該当しない場合
            } else {
                System.out.println(i);
            }
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?