有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误:表达式void inputInfo()的启动非法;

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

我的任务是:开发一个Java应用程序来计算不同类型员工的月工资。employee类型是基于父基类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

共 (2) 个答案

  1. # 1 楼答案

    有三件事不对

    1)main是类的方法inputInfo是类的方法。不能嵌套方法定义。您需要}void inputInfo之前结束main的主体。添加闭合大括号,并确保所有其他大括号按预期配对

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

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

  2. # 2 楼答案

    您的代码中存在多个问题:

    1)类别和主要方法没有结束括号

    2)方法声明不应以结尾void inputInfo();

    3)一个方法不应该在另一个方法中声明,您调用了它但不能声明。(inputInfo()在side main方法中声明)

    4)在main方法中声明的变量不应该被inputInfo();访问,因为它们对main方法是私有的

    5)如果要从main方法调用inputInfo(),则inputInfo()应声明为静态

    6)如果声明类级别,则所有变量都应声明为静态变量,或者应该是方法的私有变量,因为静态方法不能使用非静态方法/变量

    因此,更新后的代码如下所示:

    package test.file;
    
    import java.util.Date;
    import java.util.Scanner;
    
    public class PayrollSystemTest {
    
    
    
      public static void main(final String args[]) {
        inputInfo();
      }
    
      static void inputInfo() {
    
    
    
        double baseSalary;
    
      double commissionRate;
    
      int day;
    
      Date dob = new Date();
    
      double earnings;
    
      String firstName;
    
      double grossSales;
    
      double hours;
    
      Scanner input = new Scanner(System.in);
    
      String lastName;
    
      int month;
    
      String socialNum;
    
      double wage;
    
      int year;
        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();
      }
    
    }