我试图根据用户的输入确定要计算的公式。当它们都被{}关闭时它起作用,但是当整个语句关闭时,我的System.out.print不能识别它们。目前if语句周围没有括号。并且它声明已在主字符串中定义了totalSalary。感谢您的时间和意见。
if(annualSales >= 96000)
{
double totalSalary = annualPay + advCommission;
if(annualSales <= 95999)
{
double totalSalary = annualPay + commission;
//prints table for display
//Columns
System.out.print("SalesPerson\t");
System.out.print("AnnualSales\t");
System.out.print("Commission\t ");
//Columns
System.out.print("Total Salary\t");System.out.println("Difference from user");
//user input
System.out.print(firstName);System.out.print("\t\t");
System.out.println(annualSales); System.out.print(commission);
System.out.print(totalSalary);System.out.println("Difference from user");
//preset 1
System.out.print(presetSalesPerson[0]);
System.out.print("\t\t");
System.out.println(presetAnnualSales[0]); System.out.print("Commission\t ");
System.out.print(totalSalary);System.out.println("Difference from user");
//preset 2
System.out.print(presetSalesPerson[1]);System.out.print("\t\t");
System.out.println(presetAnnualSales[1]); System.out.print("Commission\t ");
System.out.print(totalSalary);System.out.println("Difference from user");
答案 0 :(得分:9)
它已经定义了totalSalary
,因为您无法创建两次变量(这是您在if语句中尝试的内容)。除了这个问题,我建议你修复大括号,然后再试一次:
double totalSalary = 0;
if(annualSales >= 96000){
totalSalary = annualPay + advCommission;
}
if(annualSales <= 95999){
totalSalary = annualPay + commission;
}
或者,以更好的格式:
double totalSalary = 0;
if(annualSales >= 96000){
totalSalary = annualPay + advCommission;
}
else{
totalSalary = annualPay + commission;
}
从if语句中提取double totalSalary
也会让您的SysOuts看到&#39;变量。
答案 1 :(得分:3)
试试这样:
double totalsalary = 0;
if(annualSales >= 96000)
{
totalSalary = annualPay + advCommission;
}
if(annualSales <= 95999)
{
totalSalary = annualPay + commission;
}
//prints table for display
//Columns
System.out.print("SalesPerson\t");System.out.print("AnnualSales\t"); System.out.print("Commission\t ");
//Columns
System.out.print("Total Salary\t");System.out.println("Difference from user");
//user input
System.out.print(firstName);System.out.print("\t\t");System.out.println(annualSales); System.out.print(commission);
System.out.print(totalSalary);System.out.println("Difference from user");
//preset 1
System.out.print(presetSalesPerson[0]);System.out.print("\t\t");System.out.println(presetAnnualSales[0]); System.out.print("Commission\t ");
System.out.print(totalSalary);System.out.println("Difference from user");
//preset 2
System.out.print(presetSalesPerson[1]);System.out.print("\t\t");System.out.println(presetAnnualSales[1]); System.out.print("Commission\t ");
System.out.print(totalSalary);System.out.println("Difference from user");