2014-02-24 39 views
0
public class Salary 
{ 
    public static void main (String[] args) 
    { 
     double currentSalary; // employee's current salary 
     double raise; // amount of the raise 
     double percentRaise; // percentage of the raise 
     double newSalary; // new salary for the employee 
     String rating; // performance rating 

     Scanner scan = new Scanner(System.in); 

     System.out.print ("Enter the current salary: "); 
     currentSalary = scan.nextDouble(); 

     System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); 
     rating = scan.next(); 
     if (rating.equals("Excellent")) 

      percentRaise = .06; 
      raise = (.06 * currentSalary); 


      else if (rating.equals("Good")) 

        percentRaise = .04; 
        raise = (.04 * currentSalary); 


        else if (rating.equals("Poor")) 

         percentRaise = .015; 
         raise = (.015 * currentSalary); 

     //Compute the raise using if ... 

     newSalary = currentSalary + raise; 

     //Print the results 
     NumberFormat money = NumberFormat.getCurrencyInstance(); 
     System.out.println(); 
     System.out.println("Current Salary: " + money.format(currentSalary)); 
     System.out.println("Amount of your raise: " + money.format(raise)); 
     System.out.println("Your new salary: " + money. format (newSalary)); 
     System.out.println(); 
     scan.close(); 
    } 
} 

如果我添加{和}其中的空白是那麼它說,提升沒有初始化。無論我做什麼,我似乎都不知道如何讓它運行。現在它告訴我刪除其他的東西讓它運行,但如果我做了,無論我寫的是優秀的,好的還是差的。它確實.015 *薪水,所以我不能得到優秀或良好的運行。任何人都知道爲什麼這些if else語句不工作?

+1

將代碼包裹在花括號中{}。這與Eclipse無關。閱讀關於變量範圍。 –

+0

如果我這樣做if(rating.equals(「Excellent」)) { percentRaise = .06; raise =(.06 * currentSalary); } else if(rating.equals(「Good」)) { percentRaise = .04; raise =(。04 * currentSalary); } else if(rating.equals(「Poor」)) { percentRaise = .015; raise =(.015 * currentSalary); } – user3344826

+0

然後我不能初始化raise – user3344826

回答

0

您必須確保如果您的if語句包含多行代碼,它將被包裹在大括號中。

public class Salary 
{ 
public static void main (String[] args) 
{ 
    double currentSalary; // employee's current salary 
    double raise; // amount of the raise 
    double percentRaise; // percentage of the raise 
    double newSalary; // new salary for the employee 
    String rating; // performance rating 

    Scanner scan = new Scanner(System.in); 

    System.out.print ("Enter the current salary: "); 
    currentSalary = scan.nextDouble(); 

    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); 
    rating = scan.next(); 
    if (rating.equals("Excellent")) 
     { 
     percentRaise = .06; 
     raise = (.06 * currentSalary); 

     } 
     else if (rating.equals("Good")) 
     { 
       percentRaise = .04; 
       raise = (.04 * currentSalary); 

      } 
       else if (rating.equals("Poor")) 
     { 
        percentRaise = .015; 
        raise = (.015 * currentSalary); 
      } 
    //Compute the raise using if ... 

    newSalary = currentSalary + raise; 

    //Print the results 
    NumberFormat money = NumberFormat.getCurrencyInstance(); 
    System.out.println(); 
    System.out.println("Current Salary: " + money.format(currentSalary)); 
    System.out.println("Amount of your raise: " + money.format(raise)); 
    System.out.println("Your new salary: " + money. format (newSalary)); 
    System.out.println(); 
    scan.close(); 
} 

}

+2

OP仍然會得到'raise is initialized'編譯器錯誤... – MadProgrammer

+0

而這段代碼的目的不明確。請添加說明,說明您已更改的內容。 – PakkuDon

+0

不知道OP是什麼意思,但是raise被聲明爲一個類級變量,並且應該能夠訪問它。如果編譯器拋出一個未初始化的,那麼我想我們可以初始化一個默認值,像0.0?應該是小菜一碟。 – Baahubali

0

您需要if語句來包裝到這些東西---> 「{}」

所以它是這樣的:

if(statement){ 
    //then do someting 
}else{ 
    //then do something else 
} 
0

只能用if沒有大括號的語句,如果它只包含一個命令,如果您有多個命令(多於一行),則需要將這些命令括在大括號中

2
if (rating.equals("Excellent")) 
    percentRaise = .06; 
    raise = (.06 * currentSalary); 
else if (rating.equals("Good")) 

不能編譯,因爲Java認爲這是...

if (rating.equals("Excellent")) 
    percentRaise = .06; 

raise = (.06 * currentSalary); 

else if (rating.equals("Good")) 

這意味着編譯器會抱怨else-if沒有if聲明。

您遇到(當你把周圍的語句{ })另一個問題是因爲Java是沒有什麼的局部變量的初始值將有決心。

這意味着Java根本不知道如何處理newSalary = currentSalary + raise;,因爲不能保證raise將具有分配給它的值。

你可以通過添加else條件的if-else塊的末尾或者乾脆提供一個初始值到本地變量克服這個...

double currentSalary = 0; // employee's current salary 
    double raise = 0; // amount of the raise 
    double percentRaise = 0; // percentage of the raise 
    double newSalary = 0; // new salary for the employee 
    String rating = ""; // performance rating 

雖然它看起來討厭,最好再讓你將不得不花費時間去調試一些完全隨機的值;

更新

記住,)區分大小寫,這意味着「優秀」不等於「優秀」。

您可以使用String#equalsIgnoreCase代替

+0

如果我初始化所有的東西然後運行,但答案總是爲0 – user3344826

+1

請記住,「equals」區分大小寫,所以如果輸入「excellent」,那麼它將不會與任何if語句匹配。你可以嘗試使用'equalsIgnoreCase'來代替。我確實運行了你的(更正後的代碼),它對我來說工作正常......只要我輸入正確的值 – MadProgrammer

+0

我也運行它,它的工作原理。雖然@MadProgrammer我注意到你的'String rating = 0;'是一個編譯錯誤。 – Radiodef

相關問題