Java:找不到符号。错误可能在方法程序中

时间:2014-05-28 21:33:09

标签: java

以下是我遇到问题的代码部分:

for (i=0; i<=numOfEmployees-1; i++)
     {
        System.out.println("Enter the name of an employee that worked this house:");
        nameOfEmployee = scan.next();
        if(numOfEmployees == 2 && numOfRooms == "6")
           nameOfEmployee.updatePay(30);
        if(numOfEmployees == 2 && numOfRooms == "5")
           nameOfEmployee.updatePay(25);
        if(numOfEmployees == 2 && numOfRooms == "4")
           nameOfEmployee.updatePay(20);
        if(numOfEmployees == 2 && numOfRooms == "3")
           nameOfEmployee.updatePay(15);
        if(numOfEmployees == 3 && numOfRooms == "6")
           nameOfEmployee.updatePay(25);
        if(numOfEmployees == 3 && numOfRooms == "5")
           nameOfEmployee.updatePay(20);
        if(numOfEmployees == 3 && numOfRooms == "4")
           nameOfEmployee.updatePay(15);
        if(numOfEmployees == 3 && numOfRooms == "3")
           nameOfEmployee.updatePay(10);
     }

这是我的方法程序:

public class Employee
{
String houseNumber, date, numOfRooms, name;
int pay;


public Employee(String name)
{
  this.name = name;
  this.pay = 0;
}

public Employee(String houseNumber, String date, String numOfRooms)
{
  this.houseNumber = houseNumber;
  this.date = date;
  this.numOfRooms = numOfRooms;
}

public void updatePay(int housePay)
{
  pay += housePay;
}

public int getPay()
{
  return pay;
}

public String getName()
{
  return name;
}

public String getHouse()
{
  return "House Number: " + houseNumber + 
     "/nDate Cleaned: " + date + 
     "/nNumber Of Rooms: " + numOfRooms;
}



}

错误是:

Calculation.java:59: error: cannot find symbol
           nameOfEmployee.updatePay(20);
                         ^
symbol:   method updatePay(int)
location: variable nameOfEmployee of type String

我为每一个updatePay都得到了它。我猜测我的方法程序有问题,但我不确定它有什么问题。

用户创建员工的代码:

  //Prompting user to enter # of employees that worked on Monday
  System.out.println("How many employees worked on Monday?");
  int numOnMon = scan.nextInt();

  Employee[] newEmployeeName = new Employee[numOnMon];

  //Prompting user to enter the employees that worked on Monday
  for (int i=0; i<=numOnMon-1; i++)
  {
     System.out.println("Please enter the name of an employee that worked on Monday:");
     name = scan.next();
     newEmployeeName[i] = new Employee(name);
  }

3 个答案:

答案 0 :(得分:1)

您正试图在updatePay上呼叫String nameOfEmployeeupdatePayString不存在Employee。创建updatePay并在其上调用{{1}}。

答案 1 :(得分:1)

我相信你的方法可以大大简化,

for (i=0; i<numOfEmployees; i++) {
  System.out.println("Enter the name of an employee that worked this house:");
  nameOfEmployee = scan.next();
  int nr = Integer.parseInt(numOfRooms);    
  if(numOfEmployees == 2) {
    if (nr >= 3 && nr <= 6) nameOfEmployee.updatePay(nr * 5);
  } else if (numOfEmployees == 3) {
    if (nr >= 3 && nr <= 6) nameOfEmployee.updatePay((nr * 5) - 5);
  }
}

答案 2 :(得分:0)

我注意到你又犯了一个错误..

当你这样做时,这是不好的

numOfRooms == "6"

== - 检查项目是否是同一个对象,在这种情况下,你不会比较字符串,而是它们的引用(指针)

您需要使用等于

检查出来:http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/