给你段例子代码 public class Teststream { public static void main(String[] args) { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); System.out.println("please input a number"); String s = ""; try { s = br.readLine(); } catch (IOException e) { System.out.println("xxxxxxxxxx"); } System.out.println(s + "-------------"); }}
不过要输入一连串的字符就长了
简单的可以是int i=System.in.read(); 及使用System.in这个类
高级用法查阅有关InputStream类的资料输出相反,分别是:System.out和OutputStream
public class Teststream {
public static void main(String[] args) {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
System.out.println("please input a number");
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
System.out.println("xxxxxxxxxx");
}
System.out.println(s + "-------------");
}}
建议系统的学习一下有关流(STREAM)方面的知识相。
也可以使用JOptionPane.showInputDialog(String prompt)来弹出一个对话框进行输入。若要进行控制台输入的话,可以使用:
Systme.in.read(byte [] b);会把读取的数据放出数组B中,如果出错,抛出IOException.用回车键结束输入,但是回车键也会当作一个输入字符输入。
String input;
try {
input = br.readLine();
System.out.println(input);
} catch(IOException ioe) {
}
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
为啥我我读出的数据不是我输入的数据?