详见代码:
public static void test(){
Scanner scanner=new Scanner(System.in);
////////读取整数/////////
int a=0,b=0;
if (scanner.hasNextLine()) //会在这里挂起程序 相当与while(true) 直到键盘输入回车
{
a=scanner.nextInt();
b=scanner.nextInt();
}
System.out.println(a+" "+b);
/*//////读取double////////*/
double c=0,d=0;
if (scanner.hasNextDouble()){
c=scanner.nextDouble();
d=scanner.nextDouble();
}
System.out.println(c+" "+d);
//////////////读取字符数组 空格分开/////////////////////////
String inputString=new String();
/*不加下面这句判断语句,就无法读取字符串,为什么???*/
if(scanner.hasNextLine())
{
scanner.nextLine();
}
inputString=scanner.nextLine();
String[] strings=inputString.split(" ");
char[] chars=new char[strings.length];
for (int i = 0; i < strings.length; i++) {
chars[i]=strings[i].charAt(0);
}
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]+" ");
}
}