我只是java編碼的初學者。java.lang.IllegalStateException:掃描程序關閉錯誤
這只是一個隨機的程序,它處於代碼的起始級別。
最近我得到這個錯誤,無法解決它。請幫我解決一下這個。
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;
}
}
程序的日期部分工作完全正常。這個計劃的第二部分是混亂的。
您已關閉掃描儀在s.close();這就是爲什麼你不能使用它 – AMB
你正在關閉掃描儀,然後使用它刪除s.close() – 2017-01-16 10:24:58
**真正**答案:去和谷歌你的異常消息**第一** – GhostCat