java 输入 高手进来看看

来源:百度知道 编辑:UC知道 时间:2024/06/09 02:00:54
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入一串字符串:");
String a;
a=sc.readLine();
错了,应该如何修改 谢谢!

import java.io.*;

public class test {
public static void main(String args[]){
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入一串字符串:");
String a;
try {
a=sc.readLine();
//System.out.println(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
应该是你的异常没有捕获,用以上的代码测试没问题了
如果你开始对于异常处理并不熟练的话,就在主函数后面加一句
public static void main(String args[])throws Exception{....}

异常没有捕获?

用Scanner比较好
第一行改成 Scanner sc = new Scanner(System.in);
就行了,还有就是别忘了import java.util.scanner

异常捕获

异常,
readLine();方法这,加个try catch