2015-04-27 277 views
2

我試圖根據用戶的輸入來確定要計算的公式。當它們都用{}關閉時它工作,但是當整個語句關閉時,我的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"); 
+0

這是因爲,變量'totalSalary'成爲了'if'塊的局部變量。在'if'塊之上聲明變量,並在裏面使用它。 –

+1

嘗試使用[if/else block](http://en.wikipedia.org/wiki/Conditional_%28computer_programming%29#If.E2.80.93then.28.E2.80.93else.29)。 –

回答

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; 
} 

拉你double totalSalary如果出來的報表也將讓你的SysOuts '看'變量。

+0

謝謝我還在學習這一點。我喜歡StackOverFlow上的所有人都對我這樣的綠色角很有幫助和善良! –

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");