英文:
Cannot add value to methods return value in java
问题
以下是您要翻译的内容:
我在每个员工子类中都有一个方法来计算他们的收入并返回该值。在我的主方法中,如果员工在工作岗位上超过5年,我想在这个值上再加200。我的方法是通过循环遍历不同员工的数组,将员工加入日期与当前日期进行比较,然后检查是否已经超过5年。如果是这样,我想获取员工的收入并在其上加200。
在employeeArray.get(i).earnings() += 200;这一行我得到一个错误 -> "需要变量"。
相关代码:
测试类
for(int i = 0; i < employeeArray.size(); i++){
    if(employeeArray.get(i).getDateJoined().compareTo(LocalDate.of(2015, 10, 16)) <= 0)
    {
        employeeArray.get(i).earnings() += 200;
    }
}
员工类
public abstract double earnings();
示例员工(老板)收入方法(员工的子类)
public double earnings() {
    try{
        if(weeklySalary < 100){
            throw new Exception();
        }
    }
    catch (Exception lowWageException){
        System.out.println();
    }
    return weeklySalary;
}
英文:
I have a method in each of my employee subclasses that calculates their earnings and returns that value.
In my main method I want to add 200 to this value if the employee has been in the workplace for over 5 years.
My approach was to compare the date that the employees joined to the current date by looping through my array of different employees and checking if it has been more than 5 years. If so, I want to get the employees earnings and add 200 to it.
I get an error at employeeArray.get(i).earnings() += 200; -> "Variable expected"
Relevant Code:
Test Class
for(int i = 0; i < employeeArray.size(); i ++){
            if(employeeArray.get(i).getDateJoined().compareTo(LocalDate.of(2015, 10, 16)) <= 0)
                {
                    employeeArray.get(i).earnings() += 200;
                }
            }
Employee Class
    public abstract double earnings();
Example employee (boss) earnings method (Subclass to Employee)
 public double earnings() {
        try{
            if(weeklySalary < 100){
                throw new Exception();
            }
        }
        catch (Exception lowWageException){
            System.out.println();
        }
        return weeklySalary;
    }
答案1
得分: 3
employeeArray.get(i).earnings() += 200;
-> "Variable expected"
Yes, some expressions can occur on the left side of an assignment, some cannot.
For example,
a = 5;
is ok, but
a + 1 = 5;
is not. A method call (like earnings()) is one of those things that cannot appear on the left side of an assignment.
For this, you need a setter (or something similar) that takes a value so you can set the earnings.
public void setEarnings( double earn ) {
weeklySalary = earn;
}
Then you can call that to set the value of weeklySalary.
if(employeeArray.get(i).getDateJoined()
.compareTo(LocalDate.of(2015, 10, 16)) <= 0)
{
double temp = employeeArray.get(i).earnings() + 200;
setEarnings( temp );
}
英文:
employeeArray.get(i).earnings() += 200; 
-> "Variable expected"
Yes, some expressions can occur on the left side of an assignment, some cannot.
For example,
a = 5;
is ok, but
a + 1 = 5;
is not.  A method call (like earnings()) is one of those things that cannot appear on the left side of an assignment.
For this, you need a setter (or something similar) that takes a value so you can set the earnings.
public void setEarnings( double earn ) {
   weeklySalary = earn;
}
Then you can call that to set the value of weeklySalary.
if(employeeArray.get(i).getDateJoined()
      .compareTo(LocalDate.of(2015, 10, 16)) <= 0)
{
   double temp = employeeArray.get(i).earnings() + 200;
   setEarnings( temp );
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论