java菜鸟请教一个代码问题

来源:百度知道 编辑:UC知道 时间:2024/05/30 11:40:09
import java.io.*;
public class KeyboardInput{
private final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public final synchronized int readInteger () {
String input = "";
int a =0;
try {
input = in.readLine();
}
catch (IOException e) {}
if (input !=null) {
try {
a =Integer.parseInt (input);
}
catch (NumberFormatException e) {}
}
return a;
}
}
这是KeyboardInput的代码
public class Lianxi2{
public static void main(final String [] args) {
KeyboardInput in =new KeyboardInput ();
String a ="" ;
do {
System.out.print("Input your number:");
a = in.readLine();
}
while (a.compareTo ("-1") !=0

KeyboardInput 中没有readLine方法, 你在KeyboardInput 中声明的方法名师应该是readInteger, 所以应该这样写:
a = Integer.toString(in.readInteger());

因为a=in.readLine(); 这句中in是类KeyboardInput的对象,但是该类并没有readline函数,