fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner scan=new Scanner(System.in);
  14. scan.useDelimiter("\n");//设置了分隔符后无法读入整数和小数,if语句判断都是false,为什么?
  15. System.out.println("请输入整数:");
  16. int num=0;
  17. if(scan.hasNextInt()){
  18. num=scan.nextInt();
  19. }else{
  20. System.out.println("输入的不是整数");
  21. scan.next();
  22. }
  23. System.out.println("请输入小数");
  24. float f=0f;
  25. if(scan.hasNextFloat()){
  26. f=scan.nextFloat();
  27. }else{
  28. System.out.println("输入的不是小数");
  29. }
  30. System.out.println("整数:"+num);
  31. System.out.println("小数"+f);
  32. }
  33. }
Success #stdin #stdout 0.17s 321344KB
stdin
1
2.1
stdout
请输入整数:
请输入小数
整数:1
小数2.1