2
1

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.

Java うるう年判定

Last updated at Posted at 2021-06-02

【Java うるう年判定】

コーディング試験を体験しました。
書きたいことが頭の中にあっても充分に書けなかった。。
ネットの記事を参考に答え合わせしたけどこんな感じか。

0年から2020年までのうるう年を判定する。

うるう年となる条件は以下の通り。
 ・4で割り切れる年はうるう年
 ・ただし、100で割り切れる年はうるう年でない。
 ・ただし、400で割り切れる年はうるう年である。

Java
public static void main(String[] args) {
 
  //0年~2020年
  for (int i = 0; i <= 2020; i++) {
 
    //年を4で割って、余り0
    if (i % 4 == 0) {
 
      //年を100で割り切れない
      if (i % 100 != 0) {
        System.out.println(i + "年は「うるう年」");
        continue;
      }

      //年を400で割って、余り0
      if (i % 400 == 0) {
        System.out.println(i + "年は「うるう年」");
      //年を400で割り切れない
      } else {
          System.out.println(i + "年は「うるう年」でない");
      }
 
    } else {
        System.out.println(i + "年は「うるう年」でない");
    }
  }
}

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
【 6/5 追記】
@fujitanozomu様が当記事のコメントにて、コードのご指摘をくださいました。
ありがとうございました。

こんなにもシンプルに書くことができます。
今回のコーディング試験の失敗を次に活かして、下記のようなコードでシンプルに書けるように自己研鑽に励みます。

Java
public static void main(String[] args) {

  //0年~2020年
  for (int i = 0; i <= 2020; i++) {
    //「年を4で割り切れる」かつ『「年を100で割り切れない」または「年を400で割り切れる」』
    if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) {
      System.out.println(i + "年は「うるう年」");
    } else {
      System.out.println(i + "年は「うるう年」でない");
    }
  }
}

おわりに。

SIerやSESでは無く、Web系の企業様で毎コードを書かれているエンジニアの方は、このようなコーディング試験は簡単に解けるのかな。
あとは、1~100での素数の判定や1~100で特定の一桁の数字が何回出力されるかの判定のコーディング試験が余裕なのかな。
自分ももっと技術力をつけて、ビジネスに貢献できるようになりたい。なる。

2
1
3

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?