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 1 year has passed since last update.

FizzBuzz問題のちょっと改良版

Last updated at Posted at 2023-02-02

いつもならifでi%3==0 %% i%5==0を入れてから
3で割り切れる場合と5で割り切れる場合を判定したが
今回は3でも5でも割り切れる場合を記述せずに両方を判定するコードを記述する。

//今回はelse ifを使わずif使う。
//3で割り切れる場合でも次の条件判定if(5で割り切れるかどうか)がスキップされないので、else ifで15入れなくてよい
public class Main {
    public static void main(String[] args) throws Exception {
        String output;//println使わずに出力するメンバ変数を決めておく。
        for (int i = 1; i <= 30; i++){
            output="";//初期値
            if (i%3==0) {
                output="Fizz";
            }
            if (i%5==0){
                output=output+"Buzz";//3でも割り切れる場合、output+"Buzz"のoutputに"Fizz"が代入される
            } 
            output=String.valueOf(i)+":"+output;
            System.out.println(output);
        }
    }
}
/*出力された値
1:
2:
3:Fizz
4:
5:Buzz
6:Fizz
7:
8:
9:Fizz
10:Buzz
11:
12:Fizz
13:
14:
15:FizzBuzz
16:
17:
18:Fizz
19:
20:Buzz
21:Fizz
22:
23:
24:Fizz
25:Buzz
26:
27:Fizz
28:
29:
30:FizzBuzz
*/
0
0
1

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?