为什么KeyboardInput()会报错?

hbliuren0 2004-10-14 08:45:12
//编写一个程序,输入10个整数计算并显示它们的和,平均值
public class Program2{
public static void main(final String[] args){
KeyboardInput input=new KeyboardInput();
System.out.println("Please enter ten integers:");
int i=1,sum=0;
double sql=0;
while(i<=10){
System.out.println("Please enter integer:");
int a=input.readInteger();
sum=sum+a;
i=i+1;
}
sql=sum/10;
System.out.println("The sum is " + sum);
System.out.println("The sql is " + sql);
}
}
...全文
161 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhigangsun 2004-10-19
  • 打赏
  • 举报
回复
嗯,支持楼上的。
skylan 2004-10-19
  • 打赏
  • 举报
回复
如果还报错误,你就把public final synchronized int readInteger();
public final synchronized long readLong();
public final synchronized double readDouble();
public final synchronized float readFloat();
public final synchronized char readCharacter();
public final synchronized String readString();
里面的final 去掉,就ok了。因为声明为final 的函数只能在本类中调用。


skylan 2004-10-19
  • 打赏
  • 举报
回复
import java.io.*;
public class KeyboardInput {
private final BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public final synchronized int readInteger(){
String input="";
int value=0;
try{
input=in.readLine();
}catch(IOException e){}
if(input!=null){
try{
value=Integer.parseInt(input);
}catch(NumberFormatException e){}
}
return value;
}
public final synchronized long readLong(){
String input="";
long value=0L;
try{
input=in.readLine();
}catch(IOException e){}
if (input!=null){
try{
value=Long.parseLong(input);
}catch(NumberFormatException e){}
}
return value;
}
public final synchronized double readDouble(){
String input="";
double value=0.0D;
try{
input=in.readLine();
}catch(IOException e){}
if(input!=null){
try{
value=Double.parseDouble(input);
}catch(NumberFormatException e){}
}
return value;
}
public final synchronized float readFloat(){
String input="";
float value=0.0F;
try{
input=in.readLine();
}catch(IOException e){}
if(input!=null){
try{
value=Float.parseFloat(input);
}catch(NumberFormatException e){}
}
return value;
}
public final synchronized char readCharacter(){
char c=' ';
try{
c=(char)in.read();
}catch(IOException e){}
return c;
}
public final synchronized String readString(){
String s="";
try{
s=in.readLine();
}catch(IOException e){}
if(s==null){
s="";
}
return s;
}
}
你在试试看能不能运行。
hbliuren0 2004-10-14
  • 打赏
  • 举报
回复
KeyboardInput的类不能在类库中调用吗?怎么写呀?
skylan 2004-10-14
  • 打赏
  • 举报
回复
KeyboardInput 类你写了吗?这个类是要自己写的,不然就不会成功运行

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧