今天,我試圖做一個基本的鍛鍊和我面臨這個問題的Java問題:與輸入
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Main.main(Main.java:14)
這是代碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double salary = 0; // Salary excluded tax
int tax = 0; // % of tax
double taxTot = 0; // amount of tax
double totSalary = 0; // Salary with tax
System.out.println("Salary, please : "); //Input salary
salary = input.nextDouble();
if (salary <= 15000) { // <=15000
tax = 10;
} else if (salary>= 40000 && salary < 60000){ // >=40000
tax = 20;
} else { // over > 60000
tax = 30;
}
taxTot = salary/100*tax;
totSalary = salary - taxTot;
System.out.println("Your tax is : " + taxTot + " Your salary : " + totSalary);
}
}
之間我編譯和運行程序的不同,你可能會得到錯誤。當我輸入一個數字時我沒有錯誤。然而,當輸入一封信時,我得到了和你一樣的錯誤,這並不奇怪。你的意見是什麼? – Gendarme
我似乎無法複製您的問題。複製和粘貼,併爲我工作得很好。 –
當您運行此應用程序時,您提供了什麼樣的輸入? – LearningPhase