java异常处理
编写一个NewLoan类,使满足以下要求:如果年利率(AnnualInterestRate,AIR)、年数(NumOfYears,NOY)或贷款总额(LoanAmount,LA)任意一项小于或等于零,则抛出IllegalArgumentException异常,若有多项不符合要求,则以最前项为准,输出格式见范例;
如果数据均符合要求,构造printTotalPayment()方法,返回总额,结果保留3位小数。
输入格式:
输入有多组数据,一个实数表示年利率,一个整数表示年数,一个实数表示贷款总额。
输出格式:
若任意一项小于或等于零,抛出IllegalArgumentException异常及相应描述(Annual interest rate must be positive.或Number of years must be positive.或Loan amount must be positive.);
有多项不符合,以不符合最前项为准;
若均符合要求,按照格式输出总额。
输入样例:
1 1 1000
2.5 0 2000
0 0 0
输出样例:
totalPayment is 1005.424
java.lang.IllegalArgumentException: Number of years must be positive.
java.lang.IllegalArgumentException: Annual interest rate must be positive.
下面是我写的代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
double AIR = input.nextDouble();
int NOY = input.nextInt();
double LA = input.nextDouble();
try {
NewLoan m = new NewLoan(AIR, NOY, LA);
System.out.printf(m.printTotalPayment());
System.out.printf("\n");
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}
/* 请在这里填写答案 */
class NewLoan{
double a,c;
int b;
NewLoan(double air,int noy,double la) throws IllegalArgumentException {
this.a=air;//年利率
this.b=noy;//年数
this.c=la;//贷款额
judge();
}
public void judge() throws IllegalArgumentException{
if(this.a<=0||(this.a<=0&&this.b<=0&&this.c<=0)){
throw new java.lang.IllegalArgumentException("Annual interest rate must be positive.");
}
else if(this.b<=0){
throw new java.lang.IllegalArgumentException("Number of years must be positive.");
}
else if(this.c<=0){
throw new java.lang.IllegalArgumentException("Loan amount must be positive.");
}
}
String printTotalPayment(){
double sum=((this.a/1200.0)/(1-(1/Math.pow(1+this.a/1200.0,12))))*12.0*this.c*this.b;
return "totalPayment is "+String.format("%.3f",sum);
}
}
class IllegalArgumentException extends Exception{
String r;
IllegalArgumentException(String r){
this.r=r;
}
@Override
public String getMessage() {
return this.r;
}
}
代码中存在以下问题:
1. 在NewLoan类的构造方法中,只是调用了judge()方法进行判断,但没有在该方法中抛出异常。应该在judge()方法中使用if条件判断,当满足条件时,抛出IllegalArgumentException异常。
2. 在Main类的main方法中,使用了错误的方法签名,应该使用String[] args而不是String args。
下面是修改后的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
double AIR = input.nextDouble();
int NOY = input.nextInt();
double LA = input.nextDouble();
try {
NewLoan m = new NewLoan(AIR, NOY, LA);
System.out.printf(m.printTotalPayment());
System.out.printf("\n");
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
}
}
}
}
class NewLoan {
private double AIR;
private int NOY;
private double LA;
NewLoan(double air, int noy, double la) throws IllegalArgumentException {
this.AIR = air;
this.NOY = noy;
this.LA = la;
judge();
}
public void judge() throws IllegalArgumentException {
if (this.AIR <= 0) {
throw new IllegalArgumentException("Annual interest rate must be positive.");
}
if (this.NOY <= 0) {
throw new IllegalArgumentException("Number of years must be positive.");
}
if (this.LA <= 0) {
throw new IllegalArgumentException("Loan amount must be positive.");
}
}
public String printTotalPayment() {
double totalPayment = LA * Math.pow(1 + AIR / 100, NOY);
return "totalPayment is " + String.format("%.3f", totalPayment);
}
}
以上代码中,使用了私有变量来存储输入的年利率、年数和贷款总额,在judge()方法中进行判断,并抛出异常。printTotalPayment()方法计算总额,并返回格式化后的字符串。在Main类的main方法中,通过try-catch语句捕获异常并输出相应信息。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]