JavaExp6异常处理加断言assert

JavaExp6异常处理

  1. 复数类中增加一个构造方法,该构造方法有两个字符串类型的参数,分别为复数的实部和虚部赋值。在构造方法里先将字符串参数转换为浮点数再赋值给实部或虚部。当字符串的内容不能转化为浮点数时,抛出数值格式异常NumberFormatException。在main函数中处理该异常,并进行测试。
    (提示:字符串转换为浮点数调用Double类提供的静态方法parseDouble,转换失败时会抛出数值格式异常NumberFormatException,在main函数中处理此异常)。
package EXP2020.exp6;
import java.io.*;

/**
 * @author JMChen
 * @date 2020/4/14
 */
class Complex {
    private double real, im;

    public Complex(String real, String im) {
        this.real = Double.parseDouble(real);
        this.im = Double.parseDouble(im);
    }

    public String toString() {
        if (im < 0)
            return "(" + this.real + "" + this.im + "i)";
        else
            return "(" + this.real + "+" + this.im + "i)";
    }

}


public class TestMainOne {
    public static void main(String[] args) {
        BufferedReader re = new BufferedReader(new InputStreamReader(System.in));

        String real = "";
        String im = "";
        System.out.println("请输入复数的实数和虚数部分:");
        while (true) {
            try {
                real = re.readLine();
                im = re.readLine();
                Complex complex = new Complex(real, im);
                System.out.println("复数为:" + complex.toString());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入的浮点格式有误,请重新输入:");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }
}

  1. 自定义非法年龄类IleageAgeException,定义一个Person类,包含年龄,姓名,性别等属性,编写属性设置和读取函数,在设置年龄函数中,判断参数是否符合要求(1~150),如果不符合则抛出异常;在main函数中用键盘输入属性值,当发生异常时要求用户重新输入,直到输入正确为止。
package EXP2020.exp6;
import java.util.Scanner;

/**
 * @author JMChen
 * @date 2020/4/14
 */
class IleageAgeException extends Exception {
    public IleageAgeException(String errorMassage) {
        super(errorMassage);
    }
}

class Person {
    protected int age;
    protected String name;
    public String sex;

    public void setAge(int age) throws IleageAgeException {
        if (age <= 0 || age > 150)
            throw new IleageAgeException("输入的年龄不符合要求");
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getSex() {
        return sex;
    }
}

public class TestMainTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Person person = new Person();
        while (true) {
            try {
                System.out.println("请依次输入年龄、姓名、性别:");
                person.setAge(sc.nextInt());
                person.setName(sc.next());
                person.setSex(sc.next());
                System.out.println("年龄:" + person.getAge());
                System.out.println("姓名:" + person.getName());
                System.out.println("性别:" + person.getSex());
                break;
            } catch (IleageAgeException e) {
                System.out.println(e.getMessage());
            }
        }

    }
}

  1. 课本174页编程题。
package EXP2020.exp6;
import java.util.*;
/**
 * @author JMChen
 * @date 2020/4/14
 */
public class TestMainThree {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);
        double sum = 0;
        int m = 0;
        while (reader.hasNextDouble()) {
            double x = reader.nextDouble();
            assert (x >= 0 && x <= 100) : "数据不合理";
            m = m + 1;
            sum = sum + x;
        }
        System.out.printf("%d个数的和为%f\n", m, sum);
        System.out.printf("%d个数的平均值是%f\n", m, sum / m);
    }
}


  • TO SUM UP

掌握java异常处理的基本方法,具备发现以及处理程序错误的能力,使应用程序具有稳定性和可靠性。

开启IDEA断言功能的方法(网上的步骤):IDEA打开断言功能

发布了7 篇原创文章 · 获赞 8 · 访问量 133

猜你喜欢

转载自blog.csdn.net/weixin_44983848/article/details/105521813