关于JAVA中的System.in

来源:百度知道 编辑:UC知道 时间:2024/05/16 14:31:15
public static void main(String[] args) {
System.out.println("请输入顾客的姓名:");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
try{
String name=br.readLine();
}catch(IOException e){
System.out.println("系统错误!");
e.printStackTrace();
}finally{
try{
is.close();
br.close();
}catch(IOException e){
System.out.println("关闭流发生错误!");
e.printStackTrace();
}
}
我想从另一个类中来联系我所输入的name,应该怎么联系?

我只在主方法时调用abc类的name变量就可以获取输入的名字啦.同样,也适用其他类里.只需要调用abc类对象的name就可以了.
import java.io.*;
public class abc{
String name;
public abc(){
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("请输入顾客的姓名:");

try{
name=br.readLine();
}catch(IOException e){
System.out.println("系统错误!");
e.printStackTrace();
}finally{
try{
is.close();
br.close();
}catch(IOException e){
System.out.println("关闭流发生错误!");
e.printStackTrace();
}
}
}

public static void main(String[] args) {
abc test=new abc();
System.out.println("你输入的名字为:"+test.name);
}

}