-1
我對這行代碼有困難。代碼是計算一個互聯網服務賬單,其中A包的費用爲10個小時9.95美元,額外的小時爲2美元。 B套餐每月花費13.95美元20小時,額外費用爲1美元。 C套餐每月需要19.95美元。在我放入小時後,我不斷收到此錯誤java.util.NoSuchElementException。java util沒有這樣的元素異常問題
下面是代碼:
import java.util.Scanner;
public class InternetServiceProvider {
public static void main (String args[])
{
while (true)
{
printMonthlyBill(calculateBill(getHours(), menu()));
}
}
public static double getHours()
{
double hours;
Scanner inputHours = new Scanner (System.in);
System.out.print("Please enter the hours used: ");
hours = inputHours.nextDouble();
inputHours.close();
return hours;
}
public static int menu()
{
int packageChoice;
Scanner userInput = new Scanner (System.in);
System.out.println("Which package have you obtain? (Please use A, B, or C)");
System.out.println("[1] Package A");
System.out.println("[2] Package B");
System.out.println("[3] Package C");
System.out.print("Please select your package: ");
packageChoice = userInput.nextInt();
userInput.close();
return packageChoice;
}
}
這是輸入:
Please enter the hours used: 25
Which package have you obtain? (Please use A, B, or C)
[1] Package A
[2] Package B
[3] Package C
Please select your package: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at InternetServiceProvider.menu(InternetServiceProvider.java:37)
at InternetServiceProvider.main(InternetServiceProvider.java:8)
歡迎來到StackOverflow。這實際上是一個重複的問題,這裏有一個答案:http://stackoverflow.com/q/13042008/1646783。 – jlars62