import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
if(sc.hasNextInt()) {
System.out.print("int");
System.out.print(" ");
}
else if(sc.hasNextBoolean()) {
System.out.print("boolean");
System.out.print(" ");
}
else if(sc.hasNextDouble()){
System.out.print("double");
System.out.print(" ");
}
else if(sc.hasNext()){
System.out.print("String\n");
}
sc.next();
//为什么hasnext 需要 next?
// 结尾空格怎么去掉?
}
}
}

next是接收字符串,如果要去除空格,可以加上trim();方法。
你用字符串接收,然后用类型尝试转不舒服吗