Class xyz()
{
HotelInfo[] b = new HotelInfo[5];
b[0] = new HotelInfo("PAUL'S CAFE","Laxmi Road");
b[1] = new HotelInfo("GEORGE","MG Road");
...
...
}
错误说“令牌上的语法错误“;”,{此令牌后预期“
Class xyz()
{
HotelInfo[] b = new HotelInfo[5];
b[0] = new HotelInfo("PAUL'S CAFE","Laxmi Road");
b[1] = new HotelInfo("GEORGE","MG Road");
...
...
}
错误说“令牌上的语法错误“;”,{此令牌后预期“
您的代码有几个问题:
尝试这个:
class xyz {
HotelInfo[] b = new HotelInfo[5];
xyz() {
b[0] = new HotelInfo("PAUL'S CAFE","Laxmi Road");
b[1] = new HotelInfo("GEORGE","MG Road");
}
}
如果您真的想在方法之外执行此操作,则可以执行
class xyz
{
HotelInfo[] b = {
new HotelInfo("PAUL'S CAFE","Laxmi Road"),
new HotelInfo("GEORGE","MG Road"),
....
new HotelInfo("ANOTHER","the last one")
};
}
如果您在类中而不是在方法中执行此操作,则出现此错误是正常的。您需要在方法中设置值,例如在构造函数中。理解 ?