在类之间传递字符串并跳过输入

时间:2014-03-24 00:27:20

标签: java string class input

所以我遇到的问题是我在课堂上要求一个人的名字。然后该函数返回getName()使用的字符串。 getName()函数将在Employee类中使用。主要问题是人员的​​姓名在被调用时没有被传递给getName()函数。第二个问题是跳过了“人的名字是什么”的输出问题。该程序不会等待用户输入任何信息,即使它已明确说明。除非我应该将我正在寻找的数据放在main()函数中传递给Employee类,否则我不知道该怎么做。

这是Person类。

import java.util.Scanner;

public class Person {

    public static void main(String[] args) {

    }

    public Person(){
    }

    //asks for the name of the person and then returns the personName for the getName function to use it
    public static String setName(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of the person? ");
        String personName = input.toString();
        return personName;
    }

    public String getName(String personName){//returns the person name to be used with an Employee object
        return personName;
    }

}

这是Employee类。

import java.util.Scanner;

public class Employee extends Person {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Person person = new Person();
        Employee emp = new Employee();

        person.setName();//calls to the person class (because an Employee object extends the parent class)

        System.out.println("What is the salary? ");//asks for salary
        double annualSalary = input.nextDouble();

        System.out.println("What is the starting year? ");//ask for the starting year
        int startYear = input.nextInt();

        System.out.println("What is the insurance number? ");//ask for insurance number
        String insuranceNum = input.toString();

        System.out.println("Name: " + person.getName(person.setName()) + //intent is to retrieve the name of the person object
                           "\nSalary: " + emp.getAnnualSalary(annualSalary) + 
                           "\nStart Year: " + emp.getStartYear(startYear) + 
                           "\nInsurance Number: " + emp.getInsuranceNum(insuranceNum));
    }

    //constructor
    public Employee(){
    }

    //returns the salary of the employee
    public double getAnnualSalary(double annualSalary){
        return annualSalary;
    }

    //returns the year the employee started
    public int getStartYear(int startYear){
        return startYear;
    }

    //returns the insurance number of the employee
    public String getInsuranceNum(String insuranceNum){
        return insuranceNum;
    }
}

2 个答案:

答案 0 :(得分:2)

  1. 我认为你误解了通常使用的吸气剂和制定者。 它们通常用于让另一个类访问存储在Person类中的数据。
  2. 不应该为getName方法提供任何参数,并返回人名:

    public String getName()
    {
      return personName;
    }
    

    通常应该为setName方法传递一个值,将personName设置为:

    public void setName(String givenName)
    {
      personName = givenName;
    }
    

    目前,Person没有将名称存储在任何地方......你需要将personName设为全局 - 将decleration置于setName方法之外。 例如:

    public class Person()
    {
      private String personName;
      // constructor, you could give a person an initial name
      public Person(String name) 
      {
        personName = name;
      } 
      // setter/getter methods below
      // ...
      //end of setter/getter methods
    }
    

    2。 正如codeNinja所说,使用input.nextLine(); 从用户那里获得意见。

答案 1 :(得分:1)

您必须使用此String personName = input.nextLine();代替String personName = input.toString();


非常好的建议 - 从static方法移除setName