2

I'm attempting to add interactive input to a program. I've been working on it for hours and can't figure it out. Here is the code -

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

I have 9 errors -

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

8 回答 8

5
Double.parseDouble (salaryString);

它的Double.parseDouble,不是double.parseDouble。:)

parseDouble方法来自Double类。

于 2012-10-03T06:35:08.547 回答
3

尝试 Double.parseDouble 而不是 double.parseDouble

于 2012-10-03T06:35:09.393 回答
1

请记住,Java 区分大小写。因此,它将 double 和 Double 视为两个单独的项目。在这种情况下,您尝试使用 Double,但错误地指定了 double。这是一个非常常见的 Java 问题,不要为此感到难过,只要注意 Java 是区分大小写的。编码快乐!!

现在,要解决问题:

线

 salary = double.parseDouble (salaryString);

应该读

 salary = Double.parseDouble (salaryString);

“numDependents = Double.parseDouble (numDependentsString);”也是如此 行也。

于 2012-10-03T06:37:59.533 回答
1

请使用 Double 而不是 double。Java 是纯区分大小写的语言。double 是一个关键字,表示变量的数据类型,没有方法。Double 是 java 中的类,具有类似 parseDouble(String str) 的方法。此方法返回字符串值的双精度值。

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

继续编码!

于 2012-10-03T18:17:14.407 回答
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。

用上面的代码替换你的代码,你会没事的。

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

于 2012-10-03T06:36:17.893 回答
0

使用双重包装类

Double.parseDouble(salaryString)
于 2012-10-03T06:36:33.323 回答
0
 salary = double.parseDouble (salaryString);

将其更改为

salary = Double.parseDouble (salaryString);
于 2012-10-03T06:37:06.597 回答
0

Java has very very helpful errors, just read them carefully.

The 19 means line number 19, then just read what it's telling you carefully. You usually don't have to read past the first few lines of the error printout. Only fix that one problem, and when you fix it, compile again and hopefully it will be a different error to solve. Repeat until you run out of errors.

I know there is a tendency to ignore the messages because they are so long, but seriously, they work.

Now, to make things easier, download Eclipse. It's free and easy--it will set up quickly. Create your program in Eclipse. It will underline every error and when you put your mouse over the error line it will tell you exactly what's wrong.

It really makes things much easier.

于 2012-10-03T06:42:14.493 回答