如何使用Scanner类解决此错误:线程中的异常" main" ...?

时间:2016-12-02 22:45:24

标签: java error-handling runtime

import java.util.Scanner;

public class Main {
  public static void main(String[]args) { 
     double num = 0;
     double counter = 0;
     double ncot = 0;
     Scanner scan = new Scanner(System.in); 

     while (!(num == -1)) {

         ncot = scan.nextDouble();
         if (ncot == -1) {
            System.out.println("The average is: " + (double)(num/counter)); 
         }
         else {
           num = num+ncot;
           counter++;
         }
     }
     scan.close();
   }
} 

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextDouble(Scanner.java:2413)

当我在Ideone.com上运行它时,它似乎触发了一个错误,但是当我在eclipse中运行它很好。

0 个答案:

没有答案