// 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; } }
Sign in to make a reply
清尘