编译代码时遇到问题

时间:2015-03-25 18:02:50

标签: java

每次我修复一些不同的错误都会发生。我的所有问题都会继续回到Scanner input = new Scanner(System.in);并导入java.util.Scanner;。我需要帮助解决这个问题,以使我的程序顺利运行。非常感谢你!

这是我的任务: 编写一个为员工建模的程序。员工有员工编号,姓名,地址和雇用日期。名称由名字和姓氏组成。地址包括街道,城市,州(2个字符)和5位邮政编码。日期由整数月,日和年组成。 在解决方案中使用Employee类,Name类,Address类和Date类。提供适当的类构造函数,getter方法,setter方法以及您认为必要的任何其他方法。 您的程序应提示用户输入多个员工的数据,然后显示该数据。存储数据的员工数量应从命令行输入。 有人可以帮我弄清楚出了什么问题!

这是我的代码:

public class Unit10
{
    public static void main( String[] args )
    }
    {
   Scanner input = new Scanner(System.in);
        int numEmployees;
        System.out.println( "How many employees do you wish to enter?" );
        numEmployees = input.nextInt();
        Employee[] employeeArray = new Employee[numEmployees]; 
        for ( int i = 0; i < numEmployees; i++ ) 
        {
            Employee e1 = new Employee();  
            Name first = new Name();
            Name last = new Name();
            System.out.println( "Enter the first name of the employee" );
            e1.setFirstName( input.nextLine() );
            System.out.println( "Enter the last name of the employee" );
            e1.setLastName( input.nextLine() );
        }
    }
}
import java.util.Scanner;
class Employee      
{
    private int number;
    private Name FirstName;
    private Name LastName;
    private Address address;
    private Date hireDate;
}
class Name
{
    private String FirstName; 
    private String LastName;
    public Name() 
    {
        FirstName = ""; 
        LastName = "";
    }
    public void setFirstName(String firstName) 
    {
        FirstName = firstName;
    }
    public void setLastName(String lastName)
    {
        LastName = lastName;
    }
    public String getFirstName() 
    {
        return FirstName;
    }
    public String getLastName()
    {
        return LastName;
    }
}
class Address
{
}
class Date
{
    private int month; 
    private int day;
    private int year;
    public Date() 
    {
        month = 0; 
        year = 0;
        day = 0;
    }
    public void setDay( int dayOfMonth ) 
    {
        day = dayOfMonth;
    }
    public void setMonth( int monthOfYear )
    {
        month = monthOfYear;
    }
    public void setYear( int whichYear )
    {
        year = whichYear;
    }
    public int getDay() 
    {
        return day;
    }
    public int getMonth() 
    {
        return month;
    }
    public int getYear() 
    {
        return year;
    }
}

当我编译它时,这是我唯一的错误: Unit10.java:22:错误:预期的类,接口或枚举 import java.util.Scanner; ^ 1错误

3 个答案:

答案 0 :(得分:1)

这似乎更好:

public class Unit10
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner(System.in);
        int numEmployees;
        System.out.println( "How many employees do you wish to enter?" );
        numEmployees = input.nextInt();
        Employee[] employeeArray = new Employee[numEmployees]; 
        for ( int i = 0; i < numEmployees; i++ ) 
        {
            Employee e1 = new Employee();  
            Name first = new Name();
            Name last = new Name();
            System.out.println( "Enter the first name of the employee" );
            e1.setFirstName( input.nextLine() );
            System.out.println( "Enter the last name of the employee" );
            e1.setLastName( input.nextLine() );
        }
    }
}

还将packageimport指令放在代码的顶部。您的代码中间会出现import指令。

答案 1 :(得分:0)

我用地址和招聘日期更新了答案:

import java.util.Scanner;

public class Unit10 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int numEmployees;
    System.out.println("How many employees do you wish to enter?");
    numEmployees = Integer.parseInt(input.nextLine());
    Employee[] employeeArray = new Employee[numEmployees];
    for (int i = 0; i < numEmployees; i++) {
        Employee e1 = new Employee();
        Name name = new Name();
        Date dateHiring = new Date();
        Address address = new Address();
        System.out.println("Enter the first name of the employee");
        name.setFirstName(input.nextLine());

        System.out.println("Enter the last name of the employee");
        name.setLastName(input.nextLine());
        e1.setName(name);

        System.out.println("Enter the Address of the employee");
        address.setFullAddress(input.nextLine());
        e1.setAddress(address);

        System.out.println("Enter the day of Hiring");
        dateHiring.setDay(Integer.parseInt(input.nextLine()));

        System.out.println("Enter the month of Hiring");
        dateHiring.setMonth(Integer.parseInt(input.nextLine()));

        System.out.println("Enter the year of Hiring");
        dateHiring.setYear(Integer.parseInt(input.nextLine()));

        e1.setHireDate(dateHiring);

        employeeArray[i] = e1;
    }

    //Print Employees
    for (Employee e: employeeArray){
        System.out.println(e.getName());
        System.out.println(e.getAddress());
        System.out.println(e.getHireDate());
    }

    input.close();
  }
}


package question29263154;

class Employee {
  private int number;
  private Name name;
  private Address address;
  private Date hireDate;

  public Name getName() {
    return name;
  }

  public void setName(Name name) {
    this.name = name;
  }

  public Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

  public Date getHireDate() {
    return hireDate;
  }

  public void setHireDate(Date hireDate) {
    this.hireDate = hireDate;
  }


}

class Name
{
  private String FirstName;
  private String LastName;
  public Name()
  {
    FirstName = "";
    LastName = "";
  }
  public void setFirstName(String firstName)
  {
    FirstName = firstName;
  }
  public void setLastName(String lastName)
  {
    LastName = lastName;
  }
  public String getFirstName()
  {
    return FirstName;
  }
  public String getLastName()
  {
    return LastName;
  }
  @Override
  public String toString() {
    return FirstName +" " + LastName;
  }
}

class Address
{
  private String fullAddress;

  public String getFullAddress() {
    return fullAddress;
  }

  public void setFullAddress(String fullAddress) {
    this.fullAddress = fullAddress;
  }

  @Override
  public String toString(){
    return fullAddress;
  }
}

class Date
{
  private int month;
  private int day;
  private int year;
  public Date()
  {
    month = 0;
    year = 0;
    day = 0;
  }
  public void setDay( int dayOfMonth )
  {
    day = dayOfMonth;
  }
  public void setMonth( int monthOfYear )
  {
    month = monthOfYear;
  }
  public void setYear( int whichYear )
  {
    year = whichYear;
  }
  public int getDay()
  {
    return day;
  }
  public int getMonth()
  {
    return month;
  }
  public int getYear()
  {
    return year;
  }

  @Override
  public String toString (){
    return String.format("%d-%d-%d", year, day, month);
  }
}

答案 2 :(得分:0)

如果您希望e1.setFirstName和e1.setLastName工作,那么Employee类应如下所示:

class Employee      
{
    private int number;
    private Name name;
    private Address address;
    private Date hireDate;

    public setFirstName(String firstName)
    {
        name.setFirstName(firstName);
    }

    public setLastName(String lastName)
    {
        name.setFirstName(lastName);
    }

    public String getFirstName()
    {
        return name.getFirstname();
    }

    public String getLastName()
    {
        return name.getLastName();
    }

}

如果您需要,您当然还必须为其他字段添加getter和setter。