[ Java練習問題 ] 練習問題 第2回 『条件分岐』

目次

if 文のバリエーションを用いた条件分岐の問題

Java練習問題 第2回では「条件分岐」についての問題を用意してあります。
「条件分岐」の解説はこちら

問題 1 難易度 ★★

① 入園料表示プログラムを作成してください。

水族館の入園料を表示するプログラムで年齢を入力すると「6 歳未満と 75 歳以上は無料」「6~12 歳は500円」「これら以外の人は1000円」と表示できるように作成してください。

実行例1

年齢を整数で入力してください。
10 // 入力する値
10歳のお客様の入園料は500円です。

実行例2

年齢を整数で入力してください。
5 // 入力する値
5歳のお客様の入園料は無料です。

問題 1 解答

正解は、、、
public class Main{
  public static void main(String[] args){
    System.out.println("年齢を整数で入力してください。");
    int age = new java.util.Scanner(System.in).nextInt();
    String fee = "";
    if (age < 6 || age >= 75) {
      fee = "無料";
    } else if (age <= 12) {
      fee = "500 円";
    } else {
      fee = "1000 円";
    }
    System.out.println(age + "歳のお客様の入園料は" + fee + "です。");
  }
}

問題 2 難易度 ★★

① 1~12 の範囲で月を入力すると、日本での季節を表示するプログラムを、「if~else if」文を用いて作成してください。
(3,4,5月は春、6,7,8月は夏、9,10,11月は秋、12,1,2月は冬を表示)

実行例

1 ~ 12 の範囲で入力してください。
3 // 入力する値
日本の 3 月は、春です。

問題 2 解答

正解は、、、
public class Main_0202 {
  public static void main(String[] args) {
    System.out.println("1~12 の範囲で月を入力してください。");
    int month = new java.util.Scanner(System.in).nextInt();
    String season = "";
    if (month < 3 || month == 12) {
      season = "";
    } else if (month < 6) {
      season = "";
    } else if (month < 9) {
      season = "";
    } else {
      season = "";
    }
  System.out.println("日本の" + month + "月は、" + season + "です。");
 }
}

switch 文を用いた条件分岐

問題 3 難易度 ★★

① 1~12 の範囲で月を入力すると、日本での季節を表示するプログラムを、
「switch」文を用いて作成してください。
(3,4,5 月は春、6,7,8 月は夏、9,10,11 月は秋、12,1,2 月は冬を表示)

実行例

1 ~ 12 の範囲で入力してください。
3 // 入力する値
日本の 3 月は、春です。

問題 3 解答

正解は、、、
public class Main_0203 {
  public static void main(String[] args) {
    System.out.println("1~12 の範囲で月を入力してください。");
    int month = new java.util.Scanner(System.in).nextInt();
    String season = "";
    switch (month) {
    case 3:
    case 4:
    case 5:
    season = "";
      break;
    case 6:
    case 7:
    case 8:
    season = "";
      break;
    case 9:
    case 10:
    case 11:
    season = "";
      break;
    case 12:
    case 1:
    case 2:
    season = "";
      break;
    default:
      break;
    }
    System.out.println("日本の" + month + "月は、" + season + "です。");
  }
}

条件分岐の応用

問題 4 難易度 ★★★

① じゃんけんゲームを作成してください。
ユーザーの手はキーボード入力、PC の手はランダムで決めてください。

実行例

じゃんけんゲーム始めるよー
PC と勝負です!
あなたの手を数字で選んでください
0:ぐー 1:ちょき 2:ぱー
1 // 入力する値
あなたの手は「ちょき」、PC の手は「ぐー」。
PC の勝ちです。

問題 4 解答

正解は、、、
public class Main_0204 {
  public static void main(String[] args) {
  System.out.println("じゃんけんゲーム始めるよー");
  System.out.println("PC と勝負です!");
  System.out.println("あなたの手を数字で選んでください");
  System.out.println("0:ぐー 1:ちょき 2:ぱー");
  int userHand = new java.util.Scanner(System.in).nextInt();
  int pcHand = new java.util.Random().nextInt(3);
  
  String strUserHand = "";
  if (userHand == 0) {
    strUserHand = "ぐー";
  } else if (userHand == 1) {
    strUserHand = "ちょき";
  } else if (userHand == 2) {
    strUserHand = "ぱー";
  }
    
  String strPcHand = "";
  if (pcHand == 0) {
    strPcHand = "ぐー";
  } else if (pcHand == 1) {
    strPcHand = "ちょき";
  } else if (pcHand == 2) {
    strPcHand = "ぱー";
  }
  
  System.out.println("あなたの手は「" + strUserHand + "」、"
    + "PC の手は「" + strPcHand + "」。");
    
  String result = "";
  switch (userHand - pcHand) {
  case -2:
  case 1:
    result = "PC の勝ち";
    break;
  case -1:
  case 2:
    result = "あなたの勝ち";
    break;
  case 0:
    result = "あいこ";
    break;
  default:
    break;
  }
  System.out.println(result + "です。");
  }
}

問題 5 難易度 ★★

① 西暦を入力すると「うるう年」か「平年」かを判定するプログラムを作成してください。

(参考)
西暦年が 4 で割り切れる年は(原則として)「うるう年」。
ただし、西暦年が 100 で割り切れる年は(原則として)「平年」。
ただし、西暦年が 400 で割り切れる年は必ず「うるう年」。

実行例

西暦年を整数で入力してください
2020 // 入力する値
2020年は、「うるう年」です

問題 5 解答

正解は、、、
public class Main_0205 {
  public static void main(String[] args) {
    System.out.println("西暦年を整数で入力してください");
    int year = new java.util.Scanner(System.in).nextInt();
    String res = "";
    if (year % 400 == 0) {
      res = "「うるう年」";
    } else if (year % 100 == 0) {
      res = "「平年」";
    } else if (year % 4 == 0) {
      res = "「うるう年」";
    } else {
      res = "「平年」";
    }
    System.out.println(year + "年は、" + res + "です");
  }
}

Java練習問題 第3回では「繰り返し処理」から出題します。

(Visited 238 times, 1 visits today)
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

目次