JavaExp6 exception handling plus assert assert

JavaExp6 exception handling

  1. A construction method is added to the complex number class. The construction method has two string-type parameters, which are assigned to the real and imaginary parts of the complex number, respectively. In the construction method, first convert the string parameter to a floating point number and assign it to the real or imaginary part. When the contents of the string cannot be converted into floating-point numbers, a NumberFormatException is thrown. Handle the exception in the main function and test it.
    (Hint: Convert the string to a floating-point number by calling the static method parseDouble provided by the Double class. When the conversion fails, a numeric format exception NumberFormatException will be thrown and handled in the main function).
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. Customize illegal age class IleageAgeException, define a Person class, including age, name, gender and other attributes, write attribute setting and reading functions, in the setting age function, determine whether the parameters meet the requirements (1 ~ 150), if not Throw an exception; use the keyboard to enter the attribute value in the main function. When an exception occurs, the user is required to re-enter until the input is correct.
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 pages of textbook programming questions.
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

Master the basic methods of java exception handling, with the ability to find and handle program errors, so that the application has stability and reliability.

How to turn on the IDEA assert function (online steps): IDEA turns on the assert function

YES
Published 7 original articles · Like 8 · Visits 133

Guess you like

Origin blog.csdn.net/weixin_44983848/article/details/105521813