java获取用户的输入分两种,一种是字符的输入,一种是整行的输入,要用到java.io包。对于字符输入来说,使用System.in方法可以输入字符;对于整行的输入,可以使用Scanner类的方法获取整行输入。
import java.io.*;
import java.util.*;
public class helloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
char c = ‘3‘;
System.out.print("hello world,please read in a character:");
try{
//只能读取输入的一个字母
c=(char)System.in.read();
}catch(IOException e){}
System.out.println("entered:" + c);
String s = "";
System.out.println("please read in a line:");
//读取输入的一行
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Scanner in = new Scanner(System.in);
s = in.next();
System.out.println("entered" + s);
}
}
int test;
Scanner sc = new Scanner(System.in);
test = sc.nextInt();
System.out.println("have entered a int:" + test);
double test1;
Scanner sc1 = new Scanner(System.in);
test1 = sc1.nextDouble();
System.out.println("have entered a double:" + test1);
Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las");
//s.useDelimiter(" |,|\\.");
while (s.hasNext()) {
System.out.println(s.next());
}
public static void main(String[] args) throws FileNotFoundException {
InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java"));
Scanner s = new Scanner(in);
while(s.hasNextLine()){
System.out.println(s.nextLine());
}
}