gpt4 book ai didi

java - 扫描仪问题

转载 作者:行者123 更新时间:2023-12-01 13:07:00 27 4
gpt4 key购买 nike

我正在制作一个程序,初始化每个员工的值,然后最终显示出来。在大约第四个循环之后,我一直遇到扫描仪问题,我收到错误 java.lang.IllegalStateException 扫描仪关闭,任何建议都会有帮助。

for(int x = 0; x < 5; x++)
{
System.out.println("For an employee who get salary enter #1.");
System.out.println("For an employee who's hourly enter #2.");
System.out.println("For an employee who's paid comission enter #3");
System.out.println("For an employee who's base & comission enter #4 or 0 to quit.");
Employees[x] = keyboard.nextInt();
switch (Employees[x])
{
case 1:
System.out.println("Please enter your first name.");
FName[x] = keyboard.next();
System.out.println("Please enter your last name.");
LName[x] = keyboard.next();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.next();
System.out.println("Please enter your salary amount $.");
Check[x] = keyboard.nextDouble();
SalariedEmployee salariedEmployee =
new SalariedEmployee( FName[x], LName[x], SS[x], Check[x] );
employees[x] = salariedEmployee;
break;
case 2:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();System.out.println("Please enter your first name.");
System.out.println("How many hours were worked?");
Hours[x] = keyboard.nextInt();
System.out.println("How much paid per hour?");
Rate[x] = keyboard.nextDouble();
HourlyEmployee hourlyEmployee =
new HourlyEmployee( FName[x], LName[x], SS[x], Hours[x], Rate[x] );
employees[x] = hourlyEmployee;
break;
case 3:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();System.out.println("Please enter your first name.");
System.out.println("What was your weekly sale?");
CommissionSales[x] = keyboard.nextDouble();
System.out.println("What is your percentage paid commission?");
CommissionRate[x] = keyboard.nextDouble();
HourlyEmployee hourlyEmployee =
new HourlyEmployee( FName[x], LName[x], SS[x], Hours[x], Rate[x] );
employees[x] = hourlyEmployee;
break;
case 4:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();
System.out.println("What was your weekly sale?");
CommissionSales[x] = keyboard.nextDouble();
System.out.println("What is your percentage paid commission?");
CommissionRate[x] = keyboard.nextDouble();
System.out.println("Please enter your salary amount $.");
Check[x] = keyboard.nextDouble();
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee( FName[x], LName[x], SS[x], CommissionSales[x], CommissionRate[x], Check[x]);
employees[x] = basePlusCommissionEmployee;
break;
}

最佳答案

如果您在尝试使用扫描仪时关闭了扫描仪,那么您使用的所有Scanner方法都会抛出IllegalStateException。在代码中的某个位置,您可能有 keyboard.close()。删除这一行,或者将其移动到程序的末尾,就可以了。来自 Java 文档:

Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException.

关于java - 扫描仪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com