Discuss / Java / 我的解答

我的解答

Topic source

清尘

#1 Created at ... [Delete] [Delete and Lock User]
// package com.itranswarp.learnjava;

/**
 * Learn Java from https://www.liaoxuefeng.com/
 * 
 * @author liaoxuefeng
 */
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) {
        // TODO: 如果传入的参数为负,则抛出 IllegalArgumentException
        try{
            if (salary<0){
                throw new IllegalArgumentException("Salary is less than 0.");
            }
            if (rate<0){
                throw new IllegalArgumentException("Rate is less than 0.");
            }
            return salary * rate;
        } catch (IllegalArgumentException e){
            e.printStackTrace();
        }
        return -1;
    }
}

  • 1

Reply