我正在參加我的第一個Java課程,正在編寫計算所得稅的程序。我得到3個相同的錯誤,說「非法表達式開始」,並指向下面的代碼中的星號。我確信我正在犯新秀錯誤,但我不知道該怎麼解決!Java錯誤:表達式的非法開始
import java.util.Scanner;
public class Lab02
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int exemptions; // Number of Exemptions
double grossSalary; // Gross Salary
double interestIncome; // Interest Income
double capitalGains; // Capital Gains
double charitableCont; // Charitable Contributions
double totalIncome; // Total Income
double adjustedIncome; // Adjusted Income
double totalTax; // Total Tax
System.out.print("Number of Exemptions: ");
exemptions = input.nextInt();
System.out.print("Gross Salary: ");
grossSalary = input.nextDouble();
System.out.print("Interest Income: ");
interestIncome = input.nextDouble();
System.out.print("Capital Gains: ");
capitalGains = input.nextDouble();
System.out.print("Charitable Contributions: ");
charitableCont = input.nextDouble();
totalIncome = (grossSalary + interestIncome);
adjustedIncome = (totalIncome - (exemptions * 1500.00) - charitableCont);
if (adjustedIncome <= 10000)
totalTax0 = 0;
if (adjustedIncome > 10000)
totalTax1 = 15% * (adjustedIncome >= 32000 ? (22000) : (adjustedIncome - 10000));
if (adjustedIncome > 32000)
totalTax2 = 23% * (adjustedIncome >= 50000 ? (18000) : (adjustedIncome - 32000));
if (adjustedIncome > 50000)
totalTax3 = 28% * (adjustedIncome - 50000);
totalTax = totalTax0 + totaltax1 + totaltax2 + totaltax3;
System.out.printf("Total Income: $%.2f%n", totalIncome); // display Total Income
System.out.printf("Adjusted Income: $%.2f%n", adjustedIncome); // display Adjusted Income
System.out.printf("Total Tax: $%.2f%n", totalTax); // display Total Tax
}
}
此外,沒有'totalTax [0-3]'變量在任何地方聲明,您需要聲明它們或重寫邏輯。 –
你的'if'語句是嵌套的,就好像你認爲它們是嵌套在一起的,但是沒有大括號'{}',所以它們不是*嵌套的,並且你的縮進是誤導性的,並且會導致代碼錯誤未來需要進行修改時。縮進應始終反映代碼結構,以便人類和Java編譯器看到相同的事物。 – Andreas