我知道以前已经回答过这样的问题,但是我似乎找不到任何分号。那么,我的代码专家怎么了?请帮忙。

我的任务是:开发一个Java应用程序,以计算许多不同类型的员工的月薪。雇员类型是在基于父基类Employee的子类数组中创建的。为每个类和驱动程序类提供了初始代码。以下是我为主要班级准备的内容。当我将inputInfo方法放在main之外,然后尝试使用它时,我收到大量错误消息,例如:

error: cannot find symbol
      earnings = input.nextDouble();
      ^
  symbol:   variable earnings
  location: class PayrollSystemTest
PayrollSystemTest.java:68: error: cannot find symbol
      earnings = input.nextDouble();
                 ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:69: error: cannot find symbol
      dob = new Date(month, day, year);
      ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:69: error: cannot find symbol
      dob = new Date(month, day, year);
                     ^
  symbol:   variable month
  location: class PayrollSystemTest
PayrollSystemTest.java:69: error: cannot find symbol
      dob = new Date(month, day, year);
                            ^
  symbol:   variable day
  location: class PayrollSystemTest
PayrollSystemTest.java:69: error: cannot find symbol
      dob = new Date(month, day, year);
                                 ^
  symbol:   variable year
  location: class PayrollSystemTest
PayrollSystemTest.java:70: error: cannot find symbol
      SalariedEmployee salariedEmployee1 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                ^
  symbol:   variable firstName
  location: class PayrollSystemTest
PayrollSystemTest.java:70: error: cannot find symbol
      SalariedEmployee salariedEmployee1 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                           ^
  symbol:   variable lastName
  location: class PayrollSystemTest
PayrollSystemTest.java:70: error: cannot find symbol
      SalariedEmployee salariedEmployee1 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                     ^
  symbol:   variable socialNum
  location: class PayrollSystemTest
PayrollSystemTest.java:70: error: cannot find symbol
      SalariedEmployee salariedEmployee1 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                                ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:70: error: cannot find symbol
      SalariedEmployee salariedEmployee1 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                                     ^
  symbol:   variable earnings
  location: class PayrollSystemTest
PayrollSystemTest.java:75: error: cannot find symbol
      earnings = input.nextDouble();
      ^
  symbol:   variable earnings
  location: class PayrollSystemTest
PayrollSystemTest.java:75: error: cannot find symbol
      earnings = input.nextDouble();
                 ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:76: error: cannot find symbol
      dob = new Date(month, day, year);
      ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:76: error: cannot find symbol
      dob = new Date(month, day, year);
                     ^
  symbol:   variable month
  location: class PayrollSystemTest
PayrollSystemTest.java:76: error: cannot find symbol
      dob = new Date(month, day, year);
                            ^
  symbol:   variable day
  location: class PayrollSystemTest
PayrollSystemTest.java:76: error: cannot find symbol
      dob = new Date(month, day, year);
                                 ^
  symbol:   variable year
  location: class PayrollSystemTest
PayrollSystemTest.java:77: error: cannot find symbol
      SalariedEmployee salariedEmployee2 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                ^
  symbol:   variable firstName
  location: class PayrollSystemTest
PayrollSystemTest.java:77: error: cannot find symbol
      SalariedEmployee salariedEmployee2 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                           ^
  symbol:   variable lastName
  location: class PayrollSystemTest
PayrollSystemTest.java:77: error: cannot find symbol
      SalariedEmployee salariedEmployee2 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                     ^
  symbol:   variable socialNum
  location: class PayrollSystemTest
PayrollSystemTest.java:77: error: cannot find symbol
      SalariedEmployee salariedEmployee2 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                                ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:77: error: cannot find symbol
      SalariedEmployee salariedEmployee2 = new SalariedEmployee(firstName, lastName, socialNum, dob, earnings);
                                                                                                     ^
  symbol:   variable earnings
  location: class PayrollSystemTest
PayrollSystemTest.java:82: error: cannot find symbol
      grossSales = input.nextDouble();
      ^
  symbol:   variable grossSales
  location: class PayrollSystemTest
PayrollSystemTest.java:82: error: cannot find symbol
      grossSales = input.nextDouble();
                   ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:84: error: cannot find symbol
      commissionRate = input.nextDouble();
      ^
  symbol:   variable commissionRate
  location: class PayrollSystemTest
PayrollSystemTest.java:84: error: cannot find symbol
      commissionRate = input.nextDouble();
                       ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:85: error: cannot find symbol
      dob = new Date(month, day, year);
      ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:85: error: cannot find symbol
      dob = new Date(month, day, year);
                     ^
  symbol:   variable month
  location: class PayrollSystemTest
PayrollSystemTest.java:85: error: cannot find symbol
      dob = new Date(month, day, year);
                            ^
  symbol:   variable day
  location: class PayrollSystemTest
PayrollSystemTest.java:85: error: cannot find symbol
      dob = new Date(month, day, year);
                                 ^
  symbol:   variable year
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                  ^
  symbol:   class CommissonEmployee
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                    ^
  symbol:   variable firstName
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                               ^
  symbol:   variable lastName
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                                         ^
  symbol:   variable socialNum
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                                                    ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                                                         ^
  symbol:   variable grossSales
  location: class PayrollSystemTest
PayrollSystemTest.java:86: error: cannot find symbol
      CommissionEmployee commissionEmployee = new CommissonEmployee(firstName, lastName, socialNum, dob, grossSales, commissonRate);
                                                                                                                     ^
  symbol:   variable commissonRate
  location: class PayrollSystemTest
PayrollSystemTest.java:91: error: cannot find symbol
      grossSales = input.nextDouble();
      ^
  symbol:   variable grossSales
  location: class PayrollSystemTest
PayrollSystemTest.java:91: error: cannot find symbol
      grossSales = input.nextDouble();
                   ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:93: error: cannot find symbol
      commissionRate = input.nextDouble();
      ^
  symbol:   variable commissionRate
  location: class PayrollSystemTest
PayrollSystemTest.java:93: error: cannot find symbol
      commissionRate = input.nextDouble();
                       ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:95: error: cannot find symbol
      baseSalary = input.nextDouble();
      ^
  symbol:   variable baseSalary
  location: class PayrollSystemTest
PayrollSystemTest.java:95: error: cannot find symbol
      baseSalary = input.nextDouble();
                   ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:96: error: cannot find symbol
      dob = new Date(month, day, year);
      ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:96: error: cannot find symbol
      dob = new Date(month, day, year);
                     ^
  symbol:   variable month
  location: class PayrollSystemTest
PayrollSystemTest.java:96: error: cannot find symbol
      dob = new Date(month, day, year);
                            ^
  symbol:   variable day
  location: class PayrollSystemTest
PayrollSystemTest.java:96: error: cannot find symbol
      dob = new Date(month, day, year);
                                 ^
  symbol:   variable year
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                             ^
  symbol:   variable firstName
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                        ^
  symbol:   variable lastName
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                                  ^
  symbol:   variable socialNum
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                                             ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                                                  ^
  symbol:   variable grossSales
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                                                              ^
  symbol:   variable commissionRate
  location: class PayrollSystemTest
PayrollSystemTest.java:97: error: cannot find symbol
      BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName, socialNum, dob, grossSales, commissionRate, baseSalary);
                                                                                                                                                              ^
  symbol:   variable baseSalary
  location: class PayrollSystemTest
PayrollSystemTest.java:102: error: cannot find symbol
      wage = input.nextDouble();
      ^
  symbol:   variable wage
  location: class PayrollSystemTest
PayrollSystemTest.java:102: error: cannot find symbol
      wage = input.nextDouble();
             ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:104: error: cannot find symbol
      hours = input.nextDouble();
      ^
  symbol:   variable hours
  location: class PayrollSystemTest
PayrollSystemTest.java:104: error: cannot find symbol
      hours = input.nextDouble();
              ^
  symbol:   variable input
  location: class PayrollSystemTest
PayrollSystemTest.java:105: error: cannot find symbol
      dob = new Date(month, day, year);
      ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:105: error: cannot find symbol
      dob = new Date(month, day, year);
                     ^
  symbol:   variable month
  location: class PayrollSystemTest
PayrollSystemTest.java:105: error: cannot find symbol
      dob = new Date(month, day, year);
                            ^
  symbol:   variable day
  location: class PayrollSystemTest
PayrollSystemTest.java:105: error: cannot find symbol
      dob = new Date(month, day, year);
                                 ^
  symbol:   variable year
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                         ^
  symbol:   variable firstName
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                                    ^
  symbol:   variable lastName
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                                              ^
  symbol:   variable socialNum
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                                                         ^
  symbol:   variable dob
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                                                              ^
  symbol:   variable wage
  location: class PayrollSystemTest
PayrollSystemTest.java:106: error: cannot find symbol
      HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, socialNum, dob, wage, hours);
                                                                                                    ^
  symbol:   variable hours
  location: class PayrollSystemTest
PayrollSystemTest.java:150: error: cannot find symbol
            CommissionEmployee employee = (CommissonEmployee) currentEmployee;
                                           ^
  symbol:   class CommissonEmployee
  location: class PayrollSystemTest
69 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


这是我的代码

import java.util.Scanner;
public class PayrollSystemTest
{

    public static void main( String args[] )
    {

        Employee employees[] = new Employee[ 5 ];
        int i = 0;
        inputInfo();
        System.out.print("What are the earnings of the salaried
                employee?");
        earnings = input.nextDouble();
        dob = new Date(month, day, year);
        SalariedEmployee salariedEmployee1 = new
            SalariedEmployee(firstName, lastName, socialNum, dob,
                    earnings);
        employees[i] = salariedEmployee1; i++;

        inputInfo();
        System.out.print("What are the earnings of the next salaried
                employee?");
        earnings = input.nextDouble();
        dob = new Date(month, day, year);
        SalariedEmployee salariedEmployee2 = new
            SalariedEmployee(firstName, lastName, socialNum, dob,
                    earnings);
        employees[i] = salariedEmployee2; i++;

        inputInfo();
        System.out.print("What are the gross sales of the commission
                employee?");
        grossSales = input.nextDouble();
        System.out.print("What is the commission rate?");
        commissionRate = input.nextDouble();
        dob = new Date(month, day, year);
        CommissionEmployee commissionEmployee = new
            CommissonEmployee(firstName, lastName, socialNum, dob,
                    grossSales, commissonRate);
        employees[i] = commissionEmployee; i++;

        inputInfo();
        System.out.print("What are the gross sales of the base plus
                commission employee?");
        grossSales = input.nextDouble();
        System.out.print("What is the commission rate?");
        commissionRate = input.nextDouble();
        System.out.print("What is the base salary of the base plus
                commission employee?");
        baseSalary = input.nextDouble();
        dob = new Date(month, day, year);
        BasePlusCommissionEmployee basePlusCommissionEmployee = new
            BasePlusCommissionEmployee(firstName, lastName,
                    socialNum,
                    dob, grossSales, commissionRate, baseSalary);
        employees[i] = basePlusCommissionEmployee; i++;

        inputInfo();
        System.out.print("What is the wage of the hourly
                employee?");
        wage = input.nextDouble();
        System.out.print("What are the hours of the hourly
                employee?");
        hours = input.nextDouble();
        dob = new Date(month, day, year);
        HourlyEmployee hourlyEmployee = new
            HourlyEmployee(firstName,
                    lastName, socialNum, dob, wage, hours);
        employees[i] = hourlyEmployee; i++;
        System.out.println( "Employees processed polymorphically:\n"
                );
        // generically process each element in array employees

        for ( Employee currentEmployee : employees )

        {

            System.out.println( currentEmployee ); // invokes
            toString

                if(currentEmployee instanceof SalariedEmployee)
                {
                    SalariedEmployee employee = (SalariedEmployee)
                        currentEmployee;
                    double oldBaseSalary = employee.getWeeklySalary();
                    employee.setWeeklySalary(4 * oldBaseSalary);
                    System.out.printf("earned: ", employee.earnings());
                }
            if(currentEmployee instanceof HourlyEmployee)
            {
                HourlyEmployee employee = (HourlyEmployee) currentEmployee;
                double oldBaseSalary = employee.getHours() *
                    employee.getWage();
                employee.setWage(4 * oldBaseSalary);
                System.out.printf("earned: ", employee.getWage());
            }
            if(currentEmployee instanceof CommissionEmployee)
            {
                CommissionEmployee employee = (CommissonEmployee)
                    currentEmployee;
                double oldBaseSalary = employee.getGrossSales() *
                    employee.getCommissionRate();
                employee.setGrossSales(4 * oldBaseSalary);
                System.out.printf("earned: ", employee.getGrossSales());
            }
            // determine whether element is a BasePlusCommissionEmployee

            if ( currentEmployee instanceof BasePlusCommissionEmployee )

            {

                // downcast Employee reference to

                // BasePlusCommissionEmployee reference

                BasePlusCommissionEmployee employee =

                    ( BasePlusCommissionEmployee ) currentEmployee;



                double oldBaseSalary = employee.getBaseSalary();

                employee.setBaseSalary( 4 * oldBaseSalary );

                System.out.printf(

                        "new base salary with 10%% increase is: $%,.2f\n",

                        employee.getBaseSalary() );

            } // end if



            System.out.printf(

                    "earned $%,.2f\n\n", currentEmployee.earnings() );

        } // end for



        // get type name of each object in employees array

        for ( int j = 0; j < employees.length; j++ )

            System.out.printf( "Employee %d is a %s\n", j,

                    employees[ j ].getClass().getName() );

    } // end main

    static void inputInfo()
    {
        Scanner input = new Scanner(System.in);
        String firstName;
        String lastName;
        String socialNum;
        double earnings;
        int month;
        int day;
        int year;
        double wage;
        double hours;
        double grossSales;
        double commissionRate;
        double baseSalary;
        Date dob = new Date();

        System.out.print("What is the first name of the employee?");
        firstName = input.next();
        System.out.print("What is the last name of the employee?");
        lastName = input.next();
        System.out.print("What is the social security number of the
                employee?");
        socialNum = input.next();
        System.out.print("What month was the employee born?");
        month = input.nextInt();
        System.out.print("What day was the employee born?");
        day = input.nextInt();
        System.out.print("What year was the employee born?");
        year = input.nextInt();
    }


} // end class PayrollSystemTest

最佳答案

有三件事是错误的。

1)main是该类的方法,而inputInfo旨在是该类的方法。您不能嵌套方法定义。您需要}main之前结束void inputInfo的正文。添加右括号,并确保所有其他括号都按预期配对。

2)所有这些数据声明看起来都应该是该类的数据成员,因此需要在main方法的声明之外。

3)void inputInfo()之后的分号错误地终止了inputInfo的定义,这使以下{ … }内容不属于任何方法,因此是非法的。删除分号。

10-07 15:48