2012-10-03 49 views
2

我正在嘗試將交互式輸入添加到程序中。我一直在研究它幾個小時,但無法弄清楚。下面是代碼 -雙變量的Java錯誤

// This program calculates an employee's take home pay. 
    public class Payrolls 
     import javax.swing.JOptionPane; 

    { 
     public static void main(String args[]) 
     { 
      String salaryString; 
      double salary; 
      double stateTax; 
      double federalTax; 
      String numDependentsString; 
      double numDependents; 
      double dependentDeduction; 
      double totalWithholding; 
      double takeHomePay; 

      salaryString = JOptionPane.showInputDialog ("Enter Salary Here: "); 
      salary = double.parseDouble (salaryString); 
      numDependentsString = JOptionPane.showInputDialog  ("Enter Number of Dependents: "); 
      numDependents = double.parseDouble (numDependentsString); 

      // Calculate state tax here. 
      stateTax = salary * .06; 

      System.out.println("State Tax: $" + stateTax); 
      // Calculate federal tax here. 
      federalTax = salary * .25; 

      System.out.println("Federal Tax: $" + federalTax); 
      // Calculate dependant deduction here. 
      dependentDeduction = (salary * .02) * numDependents; 

      System.out.println("Dependents: $" + dependentDeduction); 
      // Calculate total withholding here. 
      totalWithholding = stateTax + federalTax; 

      // Calculate take home pay here. 
      takeHomePay = salary - totalWithholding + dependentDeduction; 

      System.out.println("Salary: $" + salary); 
      System.out.println("Take Home Pay: $" + takeHomePay); 
      System.exit(0); 
     } 
    } 

我有9個錯誤 -

Payrolls.java:19: error: class expected 
       salary = double.parseDouble (salaryString); 
           ^
Payrolls.java:19: error: ';' expected 
       salary = double.parseDouble (salaryString); 
             ^
Payrolls.java:19: error: not a statement 
       salary = double.parseDouble (salaryString); 
              ^
Payrolls.java:19: error: ';' expected 
       salary = double.parseDouble (salaryString); 
                 ^
Payrolls.java:21: error: class expected 
       numDependents = double.parseDouble (numDependentsString); 
            ^
Payrolls.java:21: error: ';' expected 
       numDependents = double.parseDouble (numDependentsString); 
               ^
Payrolls.java:21: error: not a statement 
       numDependents = double.parseDouble (numDependentsString); 
                ^
Payrolls.java:21: error: ';' expected 
       numDependents = double.parseDouble (numDependentsString); 
+3

「19」表示錯誤位於第19行。 –

+1

如果您開始使用像eclipse這樣的編輯器會更好,這會讓您的生活變得輕鬆。 –

+0

無論編譯錯誤是什麼,**在任何情況下**都不應該使用浮點變量來存儲貨幣數量。使用'BigDecimal'。 – EJP

回答

5
Double.parseDouble (salaryString); 

Double.parseDouble,不double.parseDouble。 :)

parseDouble方法來自Double類。

3

嘗試Double.parseDouble不double.parseDouble

0

你沒有任何問題,其他地方則在此代碼:

salaryString = JOptionPane.showInputDialog ("Enter Salary Here: "); 
    salary = Double.parseDouble (salaryString); 
    numDependentsString = JOptionPane.showInputDialog  ("Enter Number of Dependents: "); 
    numDependents = Double.parseDouble (numDependentsString); 

而不是使用包裝類雙的,你使用double.parseDouble 。

用上面的代碼替換你的代碼,你會沒事的。

我的意思是使用Double.parseDouble來代替。

0
salary = double.parseDouble (salaryString); 

改變它使用wrapperclass到

salary = Double.parseDouble (salaryString); 
1

記得,Java是區分大小寫的。所以,它會將double和Double視爲兩個單獨的項目。在這種情況下,您正嘗試使用Double,但錯誤地指定了double。這是一個非常普遍的Java問題,不要爲此感到不滿,只需要注意Java是區分大小寫的。快樂編碼!

現在,爲了解決這個問題:

salary = double.parseDouble (salaryString); 

應該讀

salary = Double.parseDouble (salaryString); 

這同樣適用於所述 「numDependents = Double.parseDouble(numDependentsString);」線也是如此。

0

Java有非常非常有用的錯誤,請仔細閱讀它們。

19意味着行號19,然後只是讀它是什麼告訴你仔細。您通常不必閱讀錯誤打印輸出的前幾行。只解決這個問題,當你修復它時,再次編譯,希望這將是一個不同的錯誤來解決。重複,直到您用完錯誤。

我知道有一種傾向,忽略這些信息,因爲它們太長了,但是嚴重的是,它們工作。

現在,爲了讓事情更簡單,請下載Eclipse。它免費且簡單 - 它會很快建立起來。在Eclipse中創建你的程序。它會強調每一個錯誤,當你把鼠標放在錯誤行上時,它會告訴你到底發生了什麼錯誤。

它確實讓事情變得更容易。

1

而不是雙倍請使用雙。 Java是純粹的區分大小寫的語言。 double是一個關鍵字,表示變量的數據類型,沒有方法。 Double是java中的類,並具有類似parseDouble(String str)的方法。此方法返回字符串值的double值。

salary = double.parseDouble(salaryString); 
numDependentsString = JOptionPane.showInputDialog("Enter Number of Dependents: "); 
numDependents = double.parseDouble(numDependentsString); 

只是繼續編碼!