gpt4 book ai didi

java - java编译时出现Readline错误

转载 作者:行者123 更新时间:2023-12-02 13:24:14 26 4
gpt4 key购买 nike

我知道我在这里做了一些非常错误的事情,但我坦白地说,我对 java 的了解非常薄弱。每当我调用 dataIn.readLine() 时,我都会收到此编译时错误

unreported exception java.io.IOException; must be caught or declared to be thrown

这是代码,我知道命名约定很糟糕,而且它几乎没有任何作用。

import java.io.*; 
public class money {
public static void main( String[]args ){
String quarters;
String dimes;
String nickels;
String pennies;
int iquarters;
int idimes;
int inickels;
int ipennies;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

System.out.println( "Enter the number of quarters. " );
quarters = dataIn.readLine();
System.out.println( "Enter the number of dimes" );
dimes = dataIn.readLine();
System.out.println( "Enter the number of nickels" );
nickels = dataIn.readLine();
System.out.println( "Enter the number of pennies" );
pennies = dataIn.readLine();

iquarters = Integer.parseInt( quarters );
idimes = Integer.parseInt( dimes );
inickels = Integer.parseInt( nickels );
ipennies = Integer.parseInt( pennies );

}
}

http://www.ideone.com/9OM6O这里也编译了,结果是一样的。

最佳答案

更改此:

public static void main( String[]args ){

至:

public static void main( String[]args ) throws IOException {

要了解为什么需要这样做,请阅读以下内容:http://download.oracle.com/javase/tutorial/essential/exceptions/

关于java - java编译时出现Readline错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5845750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com