我試圖根據用戶的輸入來確定要計算的公式。當它們都用{}關閉時它工作,但是當整個語句關閉時,我的System.out.print無法識別它們。目前if語句中沒有括號。並且它指出totalSalary已經在主字符串中定義了。感謝您的時間和意見。Java如果其他語句錯誤
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");
這是因爲,變量'totalSalary'成爲了'if'塊的局部變量。在'if'塊之上聲明變量,並在裏面使用它。 –
嘗試使用[if/else block](http://en.wikipedia.org/wiki/Conditional_%28computer_programming%29#If.E2.80.93then.28.E2.80.93else.29)。 –