题目集1~3的总结性

发布时间 2023-05-24 22:46:51作者: 别异想天开了

  一、前言:总结三次题目集的知识点、题量、难度等情况   

题目集1:

  • 知识点:计算,单位换算,键盘录入,数组
  • 题量:9
  • 难度:中等

题目集2:

  • 知识点:键盘录入,数组,类的创建,对象的创建和使用,java日期类,递归方法,条件语句和循环语句的使用
  • 题量:4
  • 难度:偏难

题目集3:

  • 知识点:键盘录入,数组,类的创建,对象的创建和使用,接口,标准javabean
  • 题量:7
  • 难度:偏难

  二、设计与分析

题目集1:

  • 7-1: 身体质量指数(BMI)测算
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int weight = sc.nextInt();
            double height = sc.nextDouble();
            if(weight <= 0 || weight > 727 || height <= 0.0 || height > 2.72){
                System.out.println("input out of range");
                return;
            }
            double BMI = weight / (height * height);
            if(BMI < 18.5){
                System.out.println("thin");
            } else if (BMI < 24 && BMI >= 18.5) {
                System.out.println("fit");
            }else if(BMI >= 24 && BMI < 28){
                System.out.println("overweight");
            }else if (BMI >= 28){
                System.out.println("fat");
            }
        }
    }
    1. 定义变量 weight 和 height,分别存储输入的体重和身高;
    2. 对 weight 和 height 进行范围判断,判断输入是否在合理范围内,若不在则输出 "input out of range" ,并结束程序运行;
    3. 计算 BMI 值,并将其保存在变量 BMI 中;
    4. 根据 BMI 的大小任选一个分类进行匹配,输出对应的体型标准诊断:BMI<18.5时为"thin", 18.5 <=BMI<24时为"fit",24<=BMI<28时为"overweight",BMI>=28时为"fat"。
  • 问题:BMI体重正常(测试数值边界)测试点过不去

     

  • 7-2:长度质量计量单位换算
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            double quality = sc.nextDouble();
            double length = sc.nextDouble();
            if(quality < 0 || length < 0){
                System.out.println("error");
                return;
            }
            System.out.print((float)(quality/0.45359237)+" "+(float)(length/0.0254));
        }
    }
    1. 定义变量 quality 和 length,分别存储输入的英制重量和长度;
    2. 对 quality 和 length 进行判断,判断是否小于 0,若小于 0,则输出 "error" ,并结束程序运行;
    3. 将 quality 的值除以英磅和千克之间的换算系数(0.45359237),得到对应的公制重量,并将其打印出来;
    4. 将 length 的值除以英寸和米之间的换算系数(0.0254),得到对应的公制长度,并将其打印出来。
  • 问题:无
  • 7-3:奇数求和
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[10];
        int sum = 0;
        Scanner sc = new Scanner(System.in);
        for (int j = 0; j < arr.length; j++) {
            arr[j] = sc.nextInt();
        }

        for (int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 == 1 || arr[i] % 2 == -1){
                sum = sum + arr[i];
            }
        }
        System.out.println(sum);
    }
}
  1. 定义一个 int 数组 arr,用于存储用户输入的 10 个整数;
  2. 定义一个变量 sum,用于存储累加奇数的和;
  3. 利用 for 循环结构不断读取并存储用户从控制台输入的 10 个整数到数组中;
  4. 利用另一个 for 循环结构遍历数组 arr 的每个元素,判断其是否是奇数(偶数 % 2 == 0,奇数 % 2 == 1 或 -1),若是奇数则将其累加进 sum 变量中;
  5. 输出 sum 的值,即为统计后的奇数之和。
  • 问题:无
  • 7-4:房产税费计算2022
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int money = sc.nextInt()*10000;
        int estimate = sc.nextInt()*10000;
        double area = sc.nextDouble();
        double q = 0,y = 0,j = 0,c = 0;
        if(a < 0){
            return;
        }
        if(a == 1){
                    if(area <= 90){
                        q = money * 0.01;
                    }else if(area > 90 && area <= 144){
                        q = money * 0.015;
                    }
                }else if(a > 1 || area > 144){
                    q = money * 0.03;
                }
                y = money * 0.0005;
                j = area * 3;
                c = area * 1.36;
                System.out.print(q + " " + y + " " + j + " " + c);
            }
        }
  1. 从控制台读取四个需要输入的参数:a表示房屋所处位置,money表示房屋价格(元),estimate表示评估价(元),area表示房屋面积(平方米);
  2. 对输入的参数进行判断,若输入的房屋位置a小于0,则结束程序运行;
  3. .根据不同位置和面积大小计算房屋契税并赋值给变量q;
  4. 计算房屋印花税并赋值给变量y,印花税为房屋价格的0.05%;
  5. 计算房屋工本费并赋值给变量j,工本费为房屋面积的3元/平方米;
  6. 计算房屋存量资金增值税并赋值给变量c,存量资金增值税为房屋面积的1.36元/平方米;
  7. 输出计算出的四个税费结。
  • 问题:首套房正常值测试,非首套房正常值测试有点问题
  • 7-5 游戏角色选择
     1 import java.util.Scanner;
     2 public class Main{
     3     public static void main(String[] args){
     4         Scanner sc = new Scanner(System.in);
     5         int a = sc.nextInt();
     6         int b = sc.nextInt();
     7         if(a<1 || a>4 || b<1 || b>3){
     8             System.out.println("Wrong Format");
     9         }
    10         switch(a){
    11             case 1:{
    12                 if(b == 1){
    13                     System.out.print("人类"+" "+"战士");
    14                 }else if(b == 2){
    15                     System.out.print("人类"+" "+"法师");
    16                 }else if(b == 3){
    17                     System.out.print("人类"+" "+"射手");
    18                 }
    19                 break;
    20             }
    21             case 2:{
    22                 if(b == 1){
    23                     System.out.print("精灵 "+" "+"战士");
    24                 }else if(b == 2){
    25                     System.out.print("精灵"+" "+"法师");
    26                 }else if(b == 3){
    27                     System.out.print("精灵"+" "+"射手");
    28                 }
    29                 break;
    30             }case 3:{
    31                 if(b == 1){
    32                     System.out.print("兽人"+" "+"战士");
    33                 }else if(b == 2){
    34                     System.out.print("兽人"+" "+"法师");
    35                 }else if(b == 3){
    36                     System.out.print("兽人"+" "+"射手");
    37                 }
    38                 break;
    39             }case 4:{
    40                 if(b == 1){
    41                     System.out.print("暗精灵"+" "+"战士");
    42                 }else if(b == 2){
    43                     System.out.print("暗精灵"+" "+"法师");
    44                 }else if(b == 3){
    45                     System.out.print("暗精灵"+" "+"射手");
    46                 }
    47             }
    48                 break;
    49         }
    50     }
    51 }

 

  1. 从控制台读取两个需要输入的参数:a表示种族(1表示人类,2表示精灵,3表示兽人,4表示暗精灵),b表示职业(1表示战士,2表示法师,3表示射手)。
  2. 判断输入的a和b值是否在允许范围内。若不在,输出"Wrong Format"。
  3. 利用switch语句进行多重选择结构,判断种族并输出对应的职业,利用if-else结构输出对应的职业。
  4. 输出指定的种族与职业。
  • 问题:有一个正常测试格式错误

  • 7-6 学号识别:
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            int[] arr = new int[8];
            int count = 0;
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学号");
            int number = sc.nextInt();
            int temp = number;
            while (number != 0) {
                number = number / 10;
                count++;
            }
            if (count > 8 || count < 8) {
                System.out.println("Wrong Format");
                return;
            }
            for (int i = 0; i < arr.length; i++) {
                int ge = temp % 10;
                temp = temp / 10;
                arr[i] = ge;
            }
            int[] newArr = Change(arr);
            int a = newArr[2] * 10 + newArr[3];
            if (a == 1) {
                System.out.println("入学年份:20" + newArr[0] + newArr[1] + "年");
                System.out.println("学院:材料学院");
                System.out.println("班级:" + newArr[4] + newArr[5]);
                System.out.println("学号:" + newArr[6] + newArr[7]);
            } else if (a == 2) {
                System.out.println("入学年份:20" + newArr[0] + newArr[1] + "年");
                System.out.println("学院:机械学院");
                System.out.println("班级:" + newArr[4] + newArr[5]);
                System.out.println("学号:" + newArr[6] + newArr[7]);
            } else if (a == 3) {
                System.out.println("入学年份:20" + newArr[0] + newArr[1] + "年");
                System.out.println("学院:外国语学院");
                System.out.println("班级:" + newArr[4] + newArr[5]);
                System.out.println("学号:" + newArr[6] + newArr[7]);
            } else if (a == 20) {
                System.out.println("入学年份:20" + newArr[0] + newArr[1] + "年");
                System.out.println("学院:软件学院");
                System.out.println("班级:" + newArr[4] + newArr[5]);
                System.out.println("学号:" + newArr[6] + newArr[7]);
            }else{
                System.out.println("Wrong Format");
            }
        }
    
        public static int[] Change(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int tem = arr[i];
                arr[i] = arr[j];
                arr[j] = tem;
            }
            return arr;
        }
    }
    1. 使用Scanner类接收用户从控制台输入的学号。
    2. 对用户输入的学号做格式校验:验证其长度是否为8位,若不是则输出"Wrong Format"后结束程序运行;
    3. 将用户输入的数字逐位取出存放在数组中,用Change方法将数组位置颠倒存储,方便后面的解析处理。
    4. 根据传统的校园学号编码系统,将第3、4位解析出来构成入学年份的后两位,输出"入学年份:XXXX年"(X表示对应的年份数字);
    5. 将第5、6位解析出来确定所属学院,输出"学院:XX学院"(其中XX表示相应的学院名称);
    6. 将第7、8位解析出来确定班级和学号,输出"班级:XX 学号:XXX"(其中XX表示班级号码,XXX表示学生的学号);
    7. 若解析出来的结果无法匹配已知的编码方式,则输出"Wrong Format"并结束程序运行。
  • 问题:正常值测试有问题
  • 7-8 巴比伦法求平方根近似值
    import java.util.Scanner;
    public class Main{
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            float n = sc.nextFloat();
            float lastGuess = sc.nextFloat();
            if(n<0||lastGuess<0){
                System.out.println("Wrong Format");
                return;
            }
            float nextGuess = 0;
            while(true){
                nextGuess = (lastGuess+n/lastGuess)/2;
                if(Math.abs(nextGuess-lastGuess)>=0.00001){
                    lastGuess = nextGuess;
                }else{
                    break;
                }
            }
            System.out.println(lastGuess);
        }
    }
    1. 使用Scanner类接收用户从控制台输入的两个float类型的数字n和lastGuess。
    2. 对输入进行格式校验,如果n或lastGuess小于0,则输出"Wrong Format"后结束程序运行。
    3. 使用while循环来不断计算下一个猜测值nextGuess直到满足条件退出循环:将上一个猜测值lastGuess和n除以上一个猜测值lastGuess的平均值作为新的猜测值nextGuess;
    4. 每次更新猜测值之后,用Math.abs()方法计算当前猜测值nextGuess与上一个猜测值lastGuess的差距,如果小于等于指定的精度(0.00001),则跳出循环,否则将当前猜测值覆盖保存到上一个猜测值变量lastGuess中,继续下一轮循环;
    5. 最终输出得到的平方根值lastGuess。
  • 问题:非法输入问题

7-9 二进制数值提取

 
 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String args[]){
 4         Scanner sc = new Scanner(System.in);
 5        String xl=sc.nextLine();
 6         int n=xl.length();
 7         int k=0; 
 8         String zfc="";
 9         for(k=0;k<n;k++){
10             if(xl.charAt(k)=='0')
11                 zfc=zfc+xl.charAt(k);
12                 else if(xl.charAt(k)=='1')
13                    zfc=zfc+xl.charAt(k);
14             else if(xl.charAt(k)=='-'&&xl.charAt(k+1)=='1'){
15                 System.out.print(zfc);
16                 return;
17             }
18   }
19          System.out.printf("Wrong Format");
20     }
21 }

 

  1. 使用Scanner类接收用户从控制台输入的字符串。
  2. 利用String类的length()方法取得字符串长度作为循环的终止条件。
  3. 使用循环遍历字符串中的每个字符,判断是否为数字'0'或'1',如果是则把它添加到保存数字的字符串zfc中。
  4. 如果当前字符不是数字,判断其是否为特定的结束标志"-1",如果是则输出保存数字的字符串zfc,并使用return语句直接结束程序运行;否则忽略这个字符并继续遍历字符串的下一位;
  5. 最后如果没有找到结束标志,输出格式错误提示信息"Wrong Format"。
  • 问题:无
  • 7-7 判断三角形类型
     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     public static void main(String[] args) {
     5         Scanner sc = new Scanner(System.in);
     6         double a = sc.nextDouble();
     7         double b = sc.nextDouble();
     8         double c = sc.nextDouble();
     9         if ((a < 1 || a > 120) || (b < 1 || b > 120) || (c < 1 || c > 120)) {
    10             System.out.println("Wrong Format");
    11             return;
    12         }
    13         if ((a + b <= c) || (c + b <= a) || (a + c <= b)) {
    14             System.out.println("Not a triangle");
    15         } else if (a == b && a == c) {
    16             System.out.println("Equilateral triangle");
    17         } else if ((a == b && a * a + b * b == c * c) || (c == b && c * c + b * b == a * a) || (a == c && a * a + c * c == b * b)) {
    18             System.out.println("Isosceles right-angled triangle");
    19         } else if (a == b || a == c || b == c) {
    20             System.out.println("Isosceles triangle");
    21         } else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a) {
    22             System.out.println("Right-angled triangle");
    23         } else {
    24             System.out.println("General triangle");
    25         }
    26     }
    27 }
    1. 使用Scanner类接收用户从控制台输入的三个double类型数值a、b和c;
    2. 第5-7行使用if语句分别对a、b、c是否超过1~120之间的范围进行检查,如果超出则输出"Wrong Format"提示并结束程序运行;
    3. 从第8行开始,使用多重if...else if语句依次判断三角形的类型:等边三角形、等腰直角三角形、等腰三角形、直角三角形或者普通三角形,并分别输出相应的字符串表示;
    4. 如果以上所有条件都不成立,则最后默认该三角形为普通三角形,输出"General triangle"。

 

  • 问题:非三角形测试,等腰直角三角形测试有问题


题目集2: 

7-1 菜单计价程序-1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner data = new Scanner(System.in);
        Order order = new Order();
        Menu m = new Menu();
        Dish d = new Dish("西红柿炒蛋", 15);
        m.add(d);
        d = new Dish("清炒土豆丝", 12);
        m.add(d);
        d = new Dish("麻婆豆腐", 12);
        m.add(d);
        d = new Dish("油淋生菜", 9);
        m.add(d);
        while (true) {
            //System.out.println("请输入菜名");
            String dishName = data.next();
            if (dishName.equals("end"))
                break;
            int portion = data.nextInt();
           // System.out.println("请输入要1:小份,2:中份,3:大份");
            order.addARecord(dishName, portion, m);
        }
        System.out.println((int) Order.getTotalPrice());
    }
}

class Dish {
    String name;
    int unit_price;

    public Dish() {
    }

    public Dish(String name, int price) {
        this.name = name;
        this.unit_price = price;
    }

    int getPrice(int portion) {
        float[] arr = {1, 1.5f, 2};
        int endPrice = (int) (unit_price * arr[portion - 1] + 0.5);
        return endPrice;
    }
}

class Menu {
    public static Dish[] dishs;

    public void add(Dish dish) {
        int len = 0;
        if (dishs != null)
            len = dishs.length;
        Dish[] temp = new Dish[len + 1];
        if (len > 0)
            System.arraycopy(dishs, 0, temp, 0, len);
        temp[len] = dish;
        dishs = temp;
    }

    public Dish searchDish(String dishName) {
        for (int i = 0; i < dishs.length; i++) {
            if (dishs[i].name.equals(dishName))
                return dishs[i];
        }
        return null;
    }
}

class Record {
    Dish d;
    int portion;

    public Record() {
    }

    public Record(Dish d, int portion) {
        this.d = d;
        this.portion = portion;
    }

    int getPrice() {
        return d.getPrice(portion);
    }
}

class Order {
    public static Record[] records;
    Menu menu = new Menu();

    public static float getTotalPrice() {
        int total = 0;
        if (records == null) {
            return 0;
        }
        for (int i = 0; i < records.length; i++) {
            total = total + (int) records[i].getPrice();
        }
        return (int)total;
    }

    public Order() {
    }

    public Order(Menu menu) {
        this.menu = menu;
    }

    Record addARecord(String dishName, int portion, Menu menu) {
        Dish dish = menu.searchDish(dishName);
        Record record = null;
        if (dish != null) {
            record = new Record(dish, portion);
            int len = 0;
            if (records != null)
                len = records.length;
            Record temp[] = new Record[len + 1];
            if (len > 0)
                System.arraycopy(records, 0, temp, 0, len);
            temp[len] = record;
            records = temp;
        } else if (dish == null) {
            System.out.println(dishName + " " + "does not exist");
        }
        return record;
    }
}
  1. Dish类表示一道菜品,有名称name和单价unit_price两个属性,有一个getPrice方法用于根据不同份量计算实际价格;
  2. Menu类表示所有可供选择的菜品,包含一个Dish数组dishs,支持添加新菜品,提供根据菜名查找菜品的方法searchDish;
  3. Record类表示每一条点餐记录,由一个Dish对象和选择的份量portion组成,有一个getPrice方法根据菜品和份量计算单项消费金额;
  4. Order类表示整个订单,由多条Record组成,有一个Menu对象menu作为可选菜单,有一个addARecord方法用于添加一条点餐记录,同时检查 dishes 中是否存在该菜品,若不存在则输出提示信息;
  5. 在主函数Main中,先在Menu对象m中添加了四道菜品,并通过Scanner类接收用户输入的菜名和份量,使用Order的addARecord方法将数据添加到点单列表records中,重复此步骤直到用户结束(输入end),最终输出总的消费金额。

     

  • 问题:多条记录测试--含多条相同菜名的记录,记录多条相同菜名时有点问题
  • 7-2 菜单计价程序-2
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner data = new Scanner(System.in);
            Order order = new Order();
            Menu m = new Menu();
            Dish d = new Dish("西红柿炒蛋", 15);
            m.add(d);
            d = new Dish("清炒土豆丝", 12);
            m.add(d);
            d = new Dish("麻婆豆腐", 12);
            m.add(d);
            d = new Dish("油淋生菜", 9);
            m.add(d);
            while (true) {
                //System.out.println("请输入菜名");
                String dishName = data.next();
                if (dishName.equals("end"))
                    break;
                int portion = data.nextInt();
               // System.out.println("请输入要1:小份,2:中份,3:大份");
                order.addARecord(dishName, portion, m);
            }
            System.out.println((int) Order.getTotalPrice());
        }
    }
    
    class Dish {
        String name;
        int unit_price;
    
        public Dish() {
        }
    
        public Dish(String name, int price) {
            this.name = name;
            this.unit_price = price;
        }
    
        int getPrice(int portion) {
            float[] arr = {1, 1.5f, 2};
            int endPrice = (int) (unit_price * arr[portion - 1] + 0.5);
            return endPrice;
        }
    }
    
    class Menu {
        public static Dish[] dishs;
    
        public void add(Dish dish) {
            int len = 0;
            if (dishs != null)
                len = dishs.length;
            Dish[] temp = new Dish[len + 1];
            if (len > 0)
                System.arraycopy(dishs, 0, temp, 0, len);
            temp[len] = dish;
            dishs = temp;
        }
    
        public Dish searchDish(String dishName) {
            for (int i = 0; i < dishs.length; i++) {
                if (dishs[i].name.equals(dishName))
                    return dishs[i];
            }
            return null;
        }
    }
    
    class Record {
        Dish d;
        int portion;
    
        public Record() {
        }
    
        public Record(Dish d, int portion) {
            this.d = d;
            this.portion = portion;
        }
    
        int getPrice() {
            return d.getPrice(portion);
        }
    }
    
    class Order {
        public static Record[] records;
        Menu menu = new Menu();
    
        public static float getTotalPrice() {
            int total = 0;
            if (records == null) {
                return 0;
            }
            for (int i = 0; i < records.length; i++) {
                total = total + (int) records[i].getPrice();
            }
            return (int)total;
        }
    
        public Order() {
        }
    
        public Order(Menu menu) {
            this.menu = menu;
        }
    
        Record addARecord(String dishName, int portion, Menu menu) {
            Dish dish = menu.searchDish(dishName);
            Record record = null;
            if (dish != null) {
                record = new Record(dish, portion);
                int len = 0;
                if (records != null)
                    len = records.length;
                Record temp[] = new Record[len + 1];
                if (len > 0)
                    System.arraycopy(records, 0, temp, 0, len);
                temp[len] = record;
                records = temp;
            } else if (dish == null) {
                System.out.println(dishName + " " + "does not exist");
            }
            return record;
        }
    }
  1. Dish类表示一道菜品,有名称name和单价unit_price两个属性,有一个getPrice方法用于根据不同份量计算实际价格;
  2. Menu类表示所有可供选择的菜品,包含一个Dish数组dishs,支持添加新菜品,Dish[] dishs菜品数组,保存所有菜品信息,Dish searthDish(String dishName)根据菜名在菜谱中查找菜品信息,返回Dish对象。 Dish addDish(String dishName,int unit_price)添加一道菜品信息
  3. Record类表示每一条点餐记录,由一个Dish对象和选择的份量portion组成,有一个getPrice方法根据菜品和份量计算单项消费金额;
  4. Order类表示整个订单,由多条Record组成,有一个Menu对象menu作为可选菜单,有一个addARecord方法用于添加一条点餐记录,同时检查 dishes 中是否存在该菜品,若不存在则输出提示信息;
  5. 在主函数Main中,先在Menu对象m中添加了四道菜品,并通过Scanner类接收用户输入的菜名和份量,使用Order的addARecord方法将数据添加到点单列表records中,重复此步骤直到用户结束(输入end),最终输出总的消费金额。

     

  • 问题:多条订单--多条删除记录--含异常菜名--含删除记录的序号不存在,多条订单-多条删除记录-含异常菜名-含错误删除,多条订单--多条删除记录--菜单含重复菜品信息

 

  • 7-3 jmu-java-日期类的基本使用
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            String data1, data2, data3;
            int year, month, day;
            int[] arr = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            int[] brr = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            Scanner sc = new Scanner(System.in);
            data1 = sc.nextLine();
            if (data1.length() == 10) {
                year = Integer.parseInt(data1.substring(0, 4));
                month = Integer.parseInt(data1.substring(5, 7));
                day = Integer.parseInt(data1.substring(8, 10));
    
                int flag = judge(year, month, day);
                if (flag == -1) System.out.println(data1 + "无效!");
                if (flag == 1) {
                    int count = 0, t;
                    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                        System.out.println(data1 + "是闰年.");
                        for (int i = 0; i < month - 1; i++) {
                            count = count + brr[i];
                        }
                        count = count + day;
                    } else {
                        for (int i = 0; i < month - 1; i++) {
                            count = count + arr[i];
                        }
                        count = count + day;
                    }
                    t = week(year, month, day);
                    if (t == 0) {
                        t = 7;
                    }
                    System.out.println(data1 + "是当年第" + count + "天,当月第" + day + "天,当周第" + t + "天.");
                }
            } else {
                System.out.println(data1 + "无效!");
            }
            data2 = sc.next();
            data3 = sc.next();
    
            if (data2.length() == 10 && data3.length() == 10) {
                int year2 = Integer.parseInt(data2.substring(0, 4));
                int month2 = Integer.parseInt(data2.substring(5, 7));
                int day2 = Integer.parseInt(data2.substring(8, 10));
                int flag2 = judge(year2, month2, day2);
    
                int year3 = Integer.parseInt(data3.substring(0, 4));
                int month3 = Integer.parseInt(data3.substring(5, 7));
                int day3 = Integer.parseInt(data3.substring(8, 10));
                int flag3 = judge(year3, month3, day3);
                int fl = 1;
                if (flag2 == -1 || flag3 == -1) {
                    System.out.println(data2 + "或" + data3 + "中有不合法的日期.");
                    fl = 0;
                }
    
                if (flag2 == 1 && flag3 == 1) {
                    if (year2 > year3) {
                        System.out.println(data3 + "早于" + data2 + ",不合法!");
                        fl = 0;
                    }
                    if (year2 == year3 && month2 > month3) {
                        System.out.println(data3 + "早于" + data2 + ",不合法!");
                        fl = 0;
                    }
                    if (year2 == year3 && month2 == month3 && day2 > day3) {
                        System.out.println(data3 + "早于" + data2 + ",不合法!");
                        fl = 0;
                    }
    
                }
    
                if (fl == 1 && flag2 == 1 && flag3 == 1) {
                    int count2 = 0, count3 = 0, num = 0;
                    if ((year2 % 4 == 0 && year2 % 100 != 0) || year2 % 400 == 0) {
                        for (int i = 0; i < month2 - 1; i++) {
                            count2 = count2 + brr[i];
                        }
                    } else {
                        for (int i = 0; i < month2 - 1; i++) {
                            count2 = count2 + arr[i];
                        }
                    }
                    count2 = count2 + day2;
    
                    if ((year3 % 4 == 0 && year3 % 100 != 0) || year3 % 400 == 0) {
                        for (int i = 0; i < month3 - 1; i++) {
                            count3 = count3 + brr[i];
                        }
                    } else {
                        for (int i = 0; i < month3 - 1; i++) {
                            count3 = count3 + arr[i];
                        }
                    }
                    count3 = count3 + day3;
                    num = count3 - count2 + 365 * (year3 - year2) + (year3 - year2) / 4;
                    System.out.println(data3 + "与" + data2 + "之间相差" + num + "天,所在月份相差" + (month3 - month2) + ",所在年份相差" + (year3 - year2) + ".");
                }
            } else {
                System.out.println(data2 + "或" + data3 + "中有不合法的日期.");
            }
        }
    
        public static int week(int year, int month, int day) {
            int num = 0;
            if (month == 1 || month == 2) {
                month = month + 12;
                year--;
            }
            int a = year / 100;
            int b = year % 100;
            num = (a / 4) - 2 * a + b + (b / 4) + (13 * (month + 1) / 5) + day - 1;
            return num % 7;
        }
    
        public static int judge(int a, int b, int c) {
            if (b < 1 || b > 12) {
                return -1;
            } else if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0) {
                if (b == 2){
                    if (c < 1 || c > 29) {
                        return -1;
                    }
                }
            } else {
                if (b == 2) {
                    if (c < 1 || c > 28) {
                        return -1;
                    }
                }
                if (b == 1 || b == 3 || b == 5 || b == 8 || b == 7 || b == 10 || b == 12) {
                    if (c < 1 || c > 31) {
                        return -1;
                    }
                }
                if (b == 4 || b == 6 || b == 9 || b == 11) {
                    if (c < 1 || c > 30) {
                        return -1;
                    }
                }
            }
            return 1;
        }
    }

     

    1. 声明两个int型数组arr和brr,分别表示非闰年和闰年每个月份的天数;
    2. 使用Scanner类从控制台获取用户输入的三个日期数据;
    3. 对第一个日期data1进行格式校验,如果不符合yyyy-MM-dd格式,则输出无效信息;
    4. 如果格式校验通过,则将年、月、日分别转换为整型,调用judge方法判断日期格式的有效性;
    5. 如果日期有效,则执行闰年判断,如果是闰年则使用brr数组计算当年第几天,否则使用arr数组计算当年第几天;
    6. 使用week方法计算当周第几天,并输出计算结果;
    7. 对第二个和第三个日期进行格式校验和有效性判断,分别输出判断结果。
  • 问题:第二行有日期非法判断

 

  • 7-4 小明走格子
     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     public static void main(String[] args) {
     5         Scanner sc = new Scanner(System.in);
     6 
     7         int n = sc.nextInt();
     8         long[] b = new long[1000];
     9         int[] a = new int[200];
    10         b[0] = 1;
    11         b[1] = 1;
    12         b[2] = 2;
    13         b[3] = 4;
    14         b[4] = 8;
    15             for (int i = 1; i <= n; i++) {
    16                 a[i] = sc.nextInt();
    17             }
    18         for (int i = 1; i <= n; i++) {
    19             if (a[i] < 5) {
    20                 System.out.println(b[a[i]]);
    21             }else {
    22                 for (int j = 5; j <= a[i]; j++)
    23                     b[j] = 2 * b[j - 1] - b[j - 5];
    24                 System.out.println(b[a[i]]);
    25             }
    26         }
    27     }
    28 }
    1. 使用Scanner类从控制台获取一个整数n以及n个整数数据;
    2. 声明一个长整型数组b,长度为1000,并初始化前五个元素的值;
    3. 使用for循环遍历输入的n个整数数据: a. 如果当前数小于5,则直接输出数组b对应位置的值; b. 否则,使用for循环计算并更新数组b中从第5位到该数所在的位置的值,并输出该数所对应的结果。在具体计算时,使用了一个公式:B(j)=2B(j1)B(j5)。该公式能够根据当前位置的前四个数计算出后面的数值,因此可以通过这种方式一次性计算出所有需要求解的位置的值。

 

  • 题目集3

  • 7-1 菜单计价程序-3
  • 代码提交错误,没有完全写出这个题目
  • 问题:订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行有问题。

 

  • 7-2 有重复的数据
     1 import java.util.Scanner;
     2 import java.util.HashSet;
     3 import java.util.Set;
     4 public class Main {
     5     public static void main(String[] args) {
     6         Scanner sc = new Scanner(System.in);
     7         int l = sc.nextInt();
     8         sc.nextLine();
     9         String s = sc.nextLine();
    10         String[] arr = s.split(" ");
    11         Set<Integer> set = new HashSet<>();
    12         for (int i = 0; i < l; i++) {
    13             set.add(Integer.parseInt(arr[i]));
    14         }
    15         if (set.size() == l) {
    16             System.out.println("NO");
    17         } else {
    18             System.out.println("YES");
    19         }
    20     }
    21 }
    1. 使用Scanner类从控制台获取一个整数l和一个字符串s;
    2. 将字符串s按照空格分割为一个字符串数组arr;
    3. 声明一个Set对象set,并使用for循环遍历数组arr,将每个元素转换为整型之后添加到set中;
    4. 如果set中的元素个数等于l,说明所有元素都不相同,输出"NO";否则输出"YES"。
  • 问题:没有进行异常处理或者错误提示,当输入不存在数字时、输入格式不正确时等

 

  • 7-3 去掉重复的数据
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] arr = new int[100010];
            int index = 0;
            int[] temp = new int[100010];
            for (int i = 0; i < n; i++) {
                int m = sc.nextInt();
                if (arr[m] != 0) {
                    continue;
                }
                arr[m] = 1;
                temp[index] = m;
                index++;
            }
    
            for (int i = 0; i < index - 1; i++) {
                System.out.print(temp[i] + " ");
            }
            System.out.print(temp[index - 1]);
        }
    }
    1. 使用Scanner类从控制台获取一个整数n;
    2. 声明一个int型数组arr,并初始化其长度为100010;
    3. 声明一个int变量index,用于记录不重复数字的数量;
    4. 声明一个int型数组temp,并初始化其长度为100010;
    5. 使用for循环遍历n次,每次获取一个整数m,并进行以下操作: a. 如果arr[m]不等于0,说明该数字已经在数组中出现过,直接跳过该数字; b. 否则,将arr[m]置为1,表示该数字已经出现过,同时将m加入到temp数组的第index个位置,并将index值加1。
    6. 最后使用for循环遍历temp数组中前index-1个元素,并输出其对应的数字及空格,最后再输出temp数组中第index个元素对应的数字,不带空格。
  • 问题:无

 

  • 7-4 单词统计与排序
    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();
            String[] arr = str.split("\\,|\\ |\\.");
            TreeSet<String> ts = new TreeSet<>(new MyComparator());
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != null && arr[i].length() != 0) {
                    ts.add(arr[i]);
                }
            }
            Iterator it = ts.iterator();
            while (it.hasNext()) {
                String str2 = (String) it.next();
                System.out.println(str2);
            }
    
        }
    }
    
    class MyComparator implements Comparator<String> {
        @Override
        public int compare(String str1, String str2) {
            if (str1.length() > str2.length())
                return -1;
            else if (str1.length() == str2.length())
                return str1.compareToIgnoreCase(str2);
            else
                return 1;
        }
    }
    1. 使用Scanner类从控制台获取一个字符串str;
    2. 使用正则表达式 "\,|\ |\." 将字符串分割为一个字符串数组arr,其中以逗号、空格和句号作为分隔符;
    3. 声明一个TreeSet<String>类型的集合ts,并使用自定义比较器MyComparator初始化;
    4. 使用for循环遍历数组arr,将每个元素加入到ts集合中,其中需要判断当前元素是否为空或者长度为0;
    5. 使用迭代器遍历ts集合,并依次输出其中的元素。
  • 问题:无

 

  • 7-5 面向对象编程(封装性)
    import java.util.Scanner;
    public class Main{
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            //调用无参构造方法,并通过setter方法进行设值
            String sid1 = sc.next();
            String name1 = sc.next();
            int age1 = sc.nextInt();
            String major1 = sc.next();
            Student student1 = new Student();
            student1.setSid(sid1);
            student1.setName(name1);
            student1.setAge(age1);
            student1.setMajor(major1);
            //调用有参构造方法
            String sid2 = sc.next();
            String name2 = sc.next();
            int age2 = sc.nextInt();
            String major2 = sc.next();
            Student student2 = new Student(sid2, name2, age2, major2);
            //对学生student1和学生student2进行输出
            student1.print();
            student2.print();
        }
    }
    
    /* 请在这里填写答案 */
    
    
    class Student {
        private String sid;
        private String name;
        private int age;
        private String major;
    
        public Student() {
        }
    
        public Student(String sid, String name, int age, String major) {
            if(age > 0){
                this.age = age;
            }
            this.sid = sid;
            this.name = name;
            
            this.major = major;
        }
    
        public String getSid() {
            return sid;
        }
    
        public void setSid(String sid) {
            this.sid = sid;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if(age > 0){
                this.age = age;
            }
            
        }
    
        public String getMajor() {
            return major;
        }
    
        public void setMajor(String major) {
            this.major = major;
        }
    
        public void print(){
            System.out.println("学号:" + sid + ",姓名:" + name + ",年龄:" + age + ",专业:" + major);
        }
    }
    1. 在Main类中先声明一个Scanner类型的实例sc,用于读取用户输入。
    2. 首先使用无参的构造方法创建一个学生对象student1,并通过setter方法分别设值。
    3. 然后使用有参的构造方法创建一个学生对象student2。
    4. 最后在两个学生对象上调用print()方法进行输出。
    5. Student类定义了四个私有属性:学号(sid)、姓名(name)、年龄(age)、专业(major),以及它们各自的getter和setter方法,其中setAge()和setSid()方法设置了代表年龄和学号的属性值需要满足一定的条件(大于0),保证其合法性。此外,还定义了一个print()方法用于打印学生的信息。
  • 问题:无

 

  • 7-6 GPS测绘中度分秒转换
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int limit;
            int minute;
            double second;
            double result;
            limit = sc.nextInt();
            
            minute = sc.nextInt();
            
            second= sc.nextDouble();
            
            result = limit + minute / 60.0 + second / 3600.0;
    
            System.out.printf(limit + "°" + minute + "′" + second + "″" + " = ");
            System.out.printf("%.6f\n",result);
        }
    }
    1. 在Main类中先声明一个Scanner类型的实例sc,用于读取用户输入。
    2. 然后依次读入限速值、度数、分数和秒数。
    3. 根据度分秒转换公式,将度数、分数和秒数转换为以度为单位的浮点数。
    4. 最后使用System.out.printf()方法打印输出结果,其中第一个%与后面的数字配合表示占位符(这里表示整数),而%.6f则表示输出浮点数时保留6位小数。
  • 问题:无

 

  • 7-7 判断两个日期的先后,计算间隔天数、周数
    import java.time.LocalDate;
    import java.time.temporal.ChronoUnit;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            //调用Scanner这个方法
            Scanner sc = new Scanner(System.in);
            //定义两个字符串
            String str1 = sc.nextLine();
            String str2 = sc.nextLine();
            //定义两个数组
            String[] arr1 = str1.split("-");
            String[] arr2 = str2.split("-");
            //添加
            LocalDate dateOne = LocalDate.of(Integer.parseInt(arr1[0]), Integer.parseInt(arr1[1]), Integer.parseInt(arr1[2]));
            LocalDate dateTwo = LocalDate.of(Integer.parseInt(arr2[0]), Integer.parseInt(arr2[1]), Integer.parseInt(arr2[2]));
            //判断
            if (dateOne.isAfter(dateTwo)) {
                System.out.println("第一个日期比第二个日期更晚");
            } else {
                System.out.println("第一个日期比第二个日期更早");
            }
            //调用方法计算间隔天数和周数
            int day = (int) ChronoUnit.DAYS.between(dateOne, dateTwo);
            int week = (int) ChronoUnit.WEEKS.between(dateOne, dateTwo);
            System.out.println("两个日期间隔" + Math.abs(day) + "天");
            System.out.println("两个日期间隔" + Math.abs(week) + "周");
        }
    }
    1. 在Main类中先声明一个Scanner类型的实例sc,用于读取用户输入。
    2. 依次读入两个表示日期的字符串(格式为“yyyy-MM-dd”)。
    3. 将这两个字符串分别以“-”为分隔符,存入长度为3的字符串数组arr1和arr2。
    4. 使用LocalDate.of()方法将数组转换为对应的LocalDate对象dateOne和dateTwo。
    5. 判断dateOne是否比dateTwo更晚,如果是则输出“第一个日期比第二个日期更晚”,否则输出“第一个日期比第二个日期更早”。
    6. 调用ChronoUnit类的DAYS.between()方法计算两个日期之间的天数,并使用abs()方法求绝对值。
    7. 调用ChronoUnit类的WEEKS.between()方法计算两个日期之间的周数,并使用abs()方法求绝对值。
    8. 分别使用println()方法将天数和周数打印输出。
  • 问题:对日期方法使用还不熟练

  三、采坑心得

  • 在编写日期相关的程序时,需要仔细学习和理解Java API提供的相关类和方法,以确保程序的正确性和稳定性。注意一些特殊情况的处理,输入错误、异常的处理要完善,而且变量名命名要具有可读性,要更加清晰易懂以提高代码可维护性,当输入格式不正确、数组越界等情况需要进行相关判断和处理。使用纯数组来计算,没有进行任何异常处理和错误提示,容易造成数字溢出等错误,需要注意程序在处理字符时要特别小心,防止出现数组越界等问题。

  四、主要困难以及改进建议

  • 主要困难:对题目分析不够清楚,不能合理的处理类之间的相互关系,对一些没学过的不知道该如何下手
  • 明确要求和目标:在开始编码之前,需要对题目要求和程序的实现目标进行明确和理解。如果没有完全理解题目或者需求不够清晰,就容易陷入盲目编码的状态,导致代码的逻辑错误甚至无法运行。
  • 多思考和多尝试:当遇到困难时,不能轻易放弃。可以通过阅读API文档、百度搜索、询问老师同学等多种方式寻找答案和灵感,也可以尝试用不同的方法和思路去解决问题。
  • 代码注释和命名规范:良好的代码注释和合适的命名规范可以增加程序的可读性和可维护性。因此,在编写代码时应尽量遵循良好的编码习惯,将代码分块、添加注释、使用有意义的变量名等。
  • 持续改进和优化:在解决问题并编写出符合要求的程序之后,也应该考虑如何持续改进和优化代码。可以通过对代码进行重构、提高程序的健壮性、增加错误处理机制等方式来完善程序,并不断完善自己的编程技能和思想水平。

  五、总结

在本阶段中,我通过完成三次题目集的练习,对Java编程语言有了更深入的理解和认识,也学到了一些新的知识点:

  1. 在第一次题目集中,我巩固了Java基础知识,例如数据类型、运算符、条件语句和循环结构等。同时,我也掌握了字符串常用操作方法操作技能。

  2. 在第二次题目集中,我深入学习了面向对象编程的概念和Java类与对象的使用方法。包括如何定义和实例化一个对象、如何调用方法等。

  3. 在第三次题目集中,我学习了Java日期处理和异常处理两个比较实用的知识点。这些内容可以帮助我们编写更加健壮、可靠的程序。

  4. 对于日期我还要进一步学习研究。
  5. 对于多个类,我还要进一步将它们运用、使用。

对于教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见:

  1. 作业和实验设计合理,能够贯彻课堂所学,提高我们的动手编程水平。

  2. 教师可以增强课堂互动性,激发学生学习