Java,“变量名"无法解析为变量 [英] Java, "Variable name" cannot be resolved to a variable

查看:115
本文介绍了Java,“变量名"无法解析为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java 使用 Eclipse,出现此错误:

I use Eclipse using Java, I get this error:

"Variable name" cannot be resolved to a variable.

使用这个 Java 程序:

With this Java program:

public class SalCal {
    private int hoursWorked;
    public SalCal(String name, int hours, double hoursRate) {
        nameEmployee = name;
        hoursWorked = hours;
        ratePrHour = hoursRate;
    }
    public void setHoursWorked() {
        hoursWorked = hours;     //ERROR HERE, hours cannot be resolved to a type
    }
    public double calculateSalary() {
        if (hoursWorked <= 40) {
            totalSalary = ratePrHour * (double) hoursWorked;
        }
        if (hoursWorked > 40) {
            salaryAfter40 = hoursWorked - 40;
            totalSalary = (ratePrHour * 40)
                + (ratePrHour * 1.5 * salaryAfter40);
        }
        return totalSalary;
    }
}

导致此错误消息的原因是什么?

What causes this error message?

推荐答案

如果您查看变量 'hoursWorked' 的范围,您将看到它是类的成员(声明为 private int)

If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)

您遇到问题的两个变量作为参数传递给构造函数.

The two variables you are having trouble with are passed as parameters to the constructor.

错误消息是因为小时"超出了 setter 的范围.

The error message is because 'hours' is out of scope in the setter.

这篇关于Java,“变量名"无法解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆