java 为什么我会收到编译器错误,“非法的表达式开始”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17616643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting compiler errors, "illegal start of expression"?
提问by Grafica
The "illegal start of expression" errors are with these 3 statements in TestPayroll class. Thanks in advance.
“非法的表达式开始”错误与 TestPayroll 类中的这 3 个语句有关。提前致谢。
private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};
TestPayroll.java below:
TestPayroll.java 下面:
/**
This program contains a Payroll class that has private data members for an employee's full name, hourly rate pay, number of hours worked, and total pay for the week. There is an array of 3 Payroll objects, which is instantiated manually with the number of hours each employee worked. The program displays the amount of gross pay each employee has earned.
*/
import javax.swing.JOptionPane;
public class TestPayroll {
public static void main(String[] args) {
private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};
//Payroll object
Payroll calcPay = new Payroll(name, weeksPay);
//Display weekly pay
JOptionPane.showMessageDialog(null, "This week's pay for " + name[0].getName + " is " + payroll.getWeeksPay() +"\n" + "This week's pay for " + name[1].getName + " is " + payroll.getWeeksPay() + "\n" + "This week's pay for " + name[2].getName + " is " + payroll.getWeeksPay());
}
}
public class Payroll {
public static void main(String[] args) {
private String name;
private double payRate;
private double hrsWorked;
private double weeksPay;
//default constructor
public Payroll() {
this.name = name;
this.payRate = payRate;
this.hrsWorked = hrsWorked;
this.weeksPay = weeksPay;
}
//Payroll constructor
public Payroll(String name, double payRate, double hrsWorked, double weeksPay) {
this.name = name;
this.payRate = payRate;
this.hrsWorked = hrsWorked;
this.weeksPay = weeksPay;
}
//return name
public String getName() {
return name;
}
//set name
public void setName(String name) {
this.name = name;
}
//return pay rate
public double getPayRate() {
return payRate;
}
//set pay rate
public void setPayRate(double payRate) {
this.payRate = payRate;
}
//return hours worked for the week
public double getHrsWorked() {
return hrsWorked;
}
//set hours worked for the week
public void setHrsWorked(double hrsWorked) {
this.hrsWorked = hrsWorked;
}
//find week's pay
public double getWeeksPay(double weeksPay) {
double weeksPay = payRate * hrsWorked;
return weeksPay;
}
}
}
回答by Edwin Buck
public static void main(String[] args) {
is a method, not a class definition. As a result, it cannot hold members, and that means that adding a member scope private
to String name
is nonsense to the compiler.
是一个方法,而不是一个类定义。其结果是,它不能保持构件,并且这意味着添加成员范围private
至String name
是无义给编译器。
Now, if you had the following
现在,如果你有以下
public class Something {
private String name;
public static void main(String[] args) {
...
}
}
it would be a different story. Now String name
is part of the class definition, not the static method. Since it is a member of the class, it can have a class exposure scope.
那将是一个不同的故事。现在String name
是类定义的一部分,而不是静态方法。因为它是类的成员,所以它可以有一个类公开范围。
回答by Adam Arold
These belong in the class not in a method:
这些属于类而不属于方法:
private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};
or you can remove the private keyword in that case they will be local variables in your main
method.
或者您可以删除 private 关键字,在这种情况下它们将成为您main
方法中的局部变量。
Your Payroll
class has its code in a main
method. This will not compile. Remove your code from the main
method and put it in the class.
你的Payroll
类在一个main
方法中有它的代码。这不会编译。从main
方法中删除您的代码并将其放入类中。
回答by kosa
private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};
should be, just:
应该是,只是:
String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
double [] payRate = {100.25, 150.50, 124.25};
double [] hrsWorked = {40, 35, 36};
Remove private
to make them as local variables;
删除private
使它们成为局部变量;
(OR)
(或者)
If you want as instance variable, move them directly inside class.
如果你想作为实例变量,直接在类中移动它们。
回答by mprivat
Remove the private
modifier on each of those 3 lines. Or if you meant for them to be instance variables, move them out of the main
method. It depends on your intent.
删除private
这 3 行中每一行的修饰符。或者,如果您打算将它们作为实例变量,请将它们移出main
方法。这取决于你的意图。
回答by NINCOMPOOP
Remove the modifier private
:
删除修饰符private
:
String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
double [] payRate = {100.25, 150.50, 124.25};
double [] hrsWorked = {40, 35, 36};
It is an illegal modifier for local variables . Local variables can have only final
keyword preceding its declaration. Refer JLS 14.4for more.
它是局部变量的非法修饰符。局部变量在final
其声明之前只能有关键字。有关更多信息,请参阅JLS 14.4。
回答by Luca Fagioli
Using the private
modifier in a method is not legal.
private
在方法中使用修饰符是不合法的。
The private
, public
and protected
modifiers are used to control variables access among classes, not among methods. A variable declared in a method, it is accessible only in the scope of the method itself.
的private
,public
和protected
改性剂用于控制变量的类之间的访问,而不是方法之一。在方法中声明的变量,只能在方法本身的范围内访问。
You can deepen your knowledge of variable modifiers by reading the Controlling Access to Members of a Classin the official Java tutorial by Oracle.
您可以通过阅读Oracle 官方 Java 教程中的控制对类成员的访问来加深对变量修饰符的了解。