当我按Enter键作为输入时,我的程序不会打印任何内容

时间:2017-09-10 20:54:41

标签: java input enter

import java.util.NoSuchElementException;
import java.util.Scanner;
public class Test4 {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      String name = "name";
      int low = 0;
      int high = 0;
      int count = 0;
      double dnum = 0.0; 
      while(name!=null) {
         try {
            if (scanner.hasNextLine()) {
               if (scanner.hasNextInt()) {
                  low = scanner.nextInt();
                  high = scanner.nextInt();
                  count = scanner.nextInt();
                  name = null;
               } else if (scanner.hasNextDouble()) {
                  dnum = scanner.nextDouble();
                  System.out.print(dnum + " is not integer");
                  name = null;
               } else {
                  System.out.print("User enter only enter");
                  name = null;
               }
            }
         } catch (NoSuchElementException e) {
            System.out.print(dnum + "is not integer");
            name=null;
            System.out.print(low);
            System.out.print(high);
         }
      }
   scanner.close(); 
   }
}

当我按Enter作为输入时,我的程序不会打印任何内容。但是如果我用new Scanner(System.in)替换new Scanner("\n"),程序就像我预期的那样工作。有人可以向我解释一下吗?

0 个答案:

没有答案