为什么我收到“未报告的异常java.io.ioexception;必须被抓或被宣布抛出“错误? [英] Why do I receive the "unreported exception java.io.ioexception; must be caught or declared to be thrown" error?

查看:384
本文介绍了为什么我收到“未报告的异常java.io.ioexception;必须被抓或被宣布抛出“错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译程序后,我收到以下错误:



After trying to compile the program I receive the following errors:

C:\Java 1\New folder (2)\Chapter 03\Bert.java:23: unreported exception java.io.IOException; must be caught or declared to be thrown
		  custName = dataIn.readLine();
                                            ^
C:\Java 1\New folder (2)\Chapter 03\Bert.java:25: unreported exception java.io.IOException; must be caught or declared to be thrown
		  inputPrice = dataIn.readLine();
                                              ^
C:\Java 1\New folder (2)\Chapter 03\Bert.java:28: unreported exception java.io.IOException; must be caught or declared to be thrown
		  inputDownPayment = dataIn.readLine();
                                                    ^
C:\Java 1\New folder (2)\Chapter 03\Bert.java:31: unreported exception java.io.IOException; must be caught or declared to be thrown
		  inputTradeIn = dataIn.readLine();
                                                ^
C:\Java 1\New folder (2)\Chapter 03\Bert.java:34: unreported exception java.io.IOException; must be caught or declared to be thrown
		  inputMonths = dataIn.readLine();
                                               ^
C:\Java 1\New folder (2)\Chapter 03\Bert.java:37: unreported exception java.io.IOException; must be caught or declared to be thrown
		  inputAnnualInterest = dataIn.readLine();





我尝试过什么:





What I have tried:

import java.io.*;

public class Bert
{
   	public static void main(String[] args)
   	{
	   	//Declaring Variables
	   	int price, downpayment, tradeIn, months;
	   	double annualInterest, payment, interest, loanAmt;
	   	String custName="", inputPrice="",inputDownPayment="",inputTradeIn="",inputMonths="", inputAnnualInterest="";
	   	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

	   //Get Input from User
		System.out.println("What is your name?  ");
		  custName = dataIn.readLine();
		System.out.print("What is the price of the car?  ");
		  inputPrice = dataIn.readLine();
		  price = Integer.parseInt(inputPrice);
		System.out.print("What is the downpayment?  ");
		  inputDownPayment = dataIn.readLine();
		  downpayment = Integer.parseInt(inputDownPayment);
		System.out.print("What is the trade-in value?  ");
		  inputTradeIn = dataIn.readLine();
		  tradeIn = Integer.parseInt(inputTradeIn);
		System.out.print("For how many months is the loan?  ");
		  inputMonths = dataIn.readLine();
		  months = Integer.parseInt(inputMonths);
		System.out.print("What is the decimal interest rate?  ");
		  inputAnnualInterest = dataIn.readLine();
		  annualInterest =Double.parseDouble(inputAnnualInterest);

        //Calculations
		interest = annualInterest / 12;
		loanAmt = price - downpayment - tradeIn;
		payment = loanAmt / ((1 / interest) - (1 / (interest * Math.pow(1 + interest, months))));

		//Output
		System.out.print("The monthly payment for " + custName + " is $");
		System.out.println(payment);
   	}
}

推荐答案

);
System.out.println(付款);
}
}
"); System.out.println(payment); } }


您需要为方法签名添加例外

You need to add exception to method signature
public static void main(String[] args) throws IOException {


这篇关于为什么我收到“未报告的异常java.io.ioexception;必须被抓或被宣布抛出“错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆