0706 第五次作业

一、填空题
1. switch语句
2. String
3. 表达式1
4. break
5. continue

二、选择题
1. A
2. A
3. BD
4. D
5. B
6. B
7. A
8. D
9. D
10. B

三、判断题
1. T
2. T
3. F
4. T
5. T
6. F

四、简答题

1.

if语句和switch语句都是选择结构语句;

if语句是通过判断比较表达式的结果是否为真来决定是否执行后面的语句体;

switch语句则是先计算表达式的值,然后与case后面的值进行匹配,匹配成功才执行后面的语句。

2.

while语句和do...while语句都是循环语句,但while语句必须先判定条件是否成立,才能决定是否执行循环语句体,而do...语句则至少执行一次循环语句体.

3.

break语句可以结束switch语句和循环语句;

continue语句则是结束本次循环并进入下一次循环.

五、编码题

1.

import java.util.Scanner;
class Odd_Even {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int num = sc.nextInt();
        if (num % 2 == 0) {
            System.out.println(num + "是一个偶数");
        }else{
            System.out.println(num + "是一个奇数");
        }
    }
}

2.
用if语句实现:

import java.util.Scanner;
class Score_Grade_If {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生分数");
        int s = sc.nextInt();
        char g;
        if (s > 100 || s < 0) {
            System.out.println("分数输入有误");
            return;
        }else if (s >= 90) {
            g = 'A';
        }else if (s >= 80) {
            g = 'B';
        }else if (s >= 70) {
            g = 'C';
        }else if (s >= 60) {
            g = 'D';
        }else {
            g = 'E';
        }
        System.out.println("学生的成绩等级为" + g + "级");
    }
} 

用switch语句实现:

import java.util.Scanner;
class Score_Grade_Switch {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生分数");
        int s = sc.nextInt();
        if (s > 100 || s < 0) {
            System.out.println("输入有误");
            return;
        }
        s /= 10;
        char g;
        switch (s) {
            case 10:
            case 9:
                g = 'A';
            break;
            case 8:
                g = 'B';
            break;
            case 7:
                g = 'C';
            break;
            case 6:
                g = 'D';
            break;
            default:
                g = 'E';
            break;
        }
        System.out.println("学生的成绩等级为" + g);
    }
}

3.

import java.util.Scanner;
class Re {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份");
        int month = sc.nextInt();
        if (month < 1 || month > 12) {
            System.out.println("月份输入有误");
            return;
        }
        int season = month / 3;
        switch (season) {
            case 1:
                System.out.println("春天-春暖花开 春意盎然-植树 踏青");
            break;
            case 2:
                System.out.println("夏天-夏日炎炎 夏雨雨人-游泳 吃雪糕");
            break;
            case 3:
                System.out.println("秋天-秋高气爽 秋风萧瑟-登山 吃饭");
            break;
            default:
                System.out.println("冬天-千里冰封 万里雪飘-滑雪 溜冰");
            break;
        }
    }
}

4.

import java.util.Scanner;
class Score_Sum {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        for (int i = 1;i <= 5 ;i++ ) {
            System.out.println("请输入第" + i + "位学生的成绩");
            int score = sc.nextInt();
            if (score < 0 || score > 100) {
                System.out.println("输入有误,请重新输入");
                i--;
                continue;
            }
            sum += score;
        }
        System.out.println("学生的总成绩为" + sum);
    }
}

六、可选题

1.

import java.util.Scanner;
class Gift {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你的成绩,看看爸爸给你买什么");
        int score = sc.nextInt();
        if (score > 100 || score < 0){
            System.out.println("输入的成绩有问题!");
        }else if (score >= 90) {
            System.out.println("买了电脑!");
        }else if (score >= 80) {
            System.out.println("买了手机!");
        }else if (score >= 60) {
            System.out.println("请你吃饭!");
        }else {
            System.out.println("买了学习资料!");
        }
    }
}

2.

class Buy_Coke {
    public static void main(String[] args){
        int coke = 0, money = 20;
        while (money >= 3) {
            money -= 3;
            coke++;
            money++;
        }
        System.out.println("可以喝" + coke + "瓶可乐");
    }
}

猜你喜欢

转载自www.cnblogs.com/chang4/p/9275143.html