練習問題 解答

6.9 練習問題 解答

問1

① ○:if文以外にはswitch文もあります。
② ×:ifブロックの後にはelse if文を繋げることもできます。また、else文は省略可能です。
③ ○
④ ○

問2

① if(x <= 13)
② if(x > 99)
③ if(x == 7)
④ if(x >= -1)
⑤ if(x != 5)
⑥ if(x < 0)

問3

① 満たさない:x = -3 y = -4
② 満たさない:x = 3 y = 2
③ 満たさない:x = 3 y = 3
④ 満たす  :x = 3 y = 4
xよりyが大きい値でないと条件は満たされないので、④のみが条件式を満たしています。

問4

➢ Practice0601.java

1package jp.co.f1.superintro.ch06exercise;
2 
3import java.util.Scanner;
4 
5public class Practice0601 {
6    public static void main(String[] args) {
7        Scanner sc = new Scanner(System.in);
8        System.out.print("数値を入力-->");
9        int num = sc.nextInt();
10  
11        if(num % 2 == 0){
12            System.out.println("入力数値[" + num + "]は偶数です。");
13        }else{
14            System.out.println("入力数値[" + num + "]は奇数です。");
15        }
16    }
17 }

NEXT>> 第7章 繰り返し処理

f