Discuss / Java / 练习:如果传入的参数为负,则抛出IllegalArgumentException

练习:如果传入的参数为负,则抛出IllegalArgumentException

Topic source

韦雪松

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {
    public static void main(String[] args) {
        System.out.println(tax(2000, 0.1));
        System.out.println(tax(-200, 0.1));
        System.out.println(tax(2000, -0.1));
    }

    static double tax(int salary, double rate) throws IllegalArgumentException {
        // TODO: 如果传入的参数为负,则抛出IllegalArgumentException
        if (salary < 0 || rate < 0) {
            throw new IllegalArgumentException("传入的参数为负");
        }
        return salary * rate;
    }
}

  • 1

Reply