java.lang.IllegalStateException: 扫描仪关闭错误

java.lang.IllegalStateException: Scanner closed ERROR

我只是 java 编码的初学者。

这只是一个处于代码起始级别的随机程序。

最近我遇到了这个错误,但无法解决。请帮我解决这个问题。

Exception in thread "main" Enter the first number:java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at New.main(New.java:19)

我的程序如下:

import java.util.*;
public class New {
    public static void main(String args[]){
        int hour,min,sec,o;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the time:");
        hour=s.nextInt();
        min=s.nextInt();
        sec=s.nextInt();

        date  d = new date();
        d.setdate(hour,min,sec);
        System.out.println(d.display());
        s.close();

        calc c = new calc();
        int a,b;
        System.out.print("Enter the first number:");
        a=s.nextInt();
        System.out.print("Enter the second number:");
        b=s.nextInt();
        c.get(a,b);
        System.out.println("Which operation do you want to perform");
        System.out.println("1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division");
        System.out.print("Enter the Operation:");
        o=s.nextInt();
        c.compute(o);

    }

    public class date {
    int hour,min,sec;

    public void setdate(int h,int m,int s){
        hour= ((h>0 && h<24)?h:0);
        min= ((m>0 && m<60)?m:0);
        sec= ((s>0 && s<60)?s:0);
    }
    public String display(){
        return String.format("%02d:%02d:%02d",hour,min,sec);
    }}



}
public class calc {
    int a,b,o;
    double c;
    public void get(int x,int y){
        a=x;
        b=y;
    }
    public double compute(int z){
        o=z;
    switch(o)
    {
    case 1: c=a+b;
    break;
    case 2: c=a-b;
    break;
    case 3: c=a*b;
    break;
    case 4: c=a/b;
    break;
    default: System.out.println("Invalid Operation");
    }
    return c;
    }
}

程序的日期部分工作得很好。乱七八糟的是节目的第二部分。

    ...
    d.setdate(hour,min,sec);
    System.out.println(d.display());
    s.close();
    ...

删除语句

    s.close();

不要关闭输入流,否则将无法再次读取。

 s.close();

显示计算输出:

double d = c.compute(o);
System.out.print("Result:" +d);

您关闭扫描仪,然后在其上调用 nextInt

 s.close(); //remove this line

 calc c = new calc();
    int a,b;
    System.out.print("Enter the first number:");
    a=s.nextInt();

所以删除 s.close();,您的代码将起作用