[英]Java error: illegal start of expression
我正在学习我的第一门 Java 课程,并且正在编写一个计算所得税的程序。 我收到 3 个相同的错误,指出“非法的表达式开始”并指向以下代码中的星号。 我确定我犯了一个新手错误,但我不知道要解决什么问题!
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
}
}
Java 中没有百分比运算符%
。 Java 将%
视为余数(有点像模数)运算符,它不像您认为的那样工作。
所以做0.15
或15.0 / 100
,或类似的东西而不是15%
例如,这个:
totalTax1 = 15% * ( adjustedIncome >= 32000 ? (22000) : (adjustedIncome - 10000) );
变成
totalTax1 = 0.15 * ( adjustedIncome >= 32000 ? (22000) : (adjustedIncome - 10000) );
这是一个简单的错误。 我使用模数运算符作为百分比符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.