我是Java的新手。我得到了“非法表达错误”。我一直在寻找答案而且无法找到答案,如果我使用括号不正确但是我已经尝试过它而没有括号和它们并且似乎无法通过这个1错误。我可以使用一些帮助。 谢谢:))
public class LIANGLAB1
{
public static void main(String[] argv){
gasStation A = new gasStation(3.39, 3.49);
gasStation B = new gasStation(3.19, 3.39);
A.sellregular(10); A.sellregular(10);
B.sellregular(11); B.sellregular(12);
if (A.moreprofit(B)) System.out.println("station A is more profitable");
else System.out.println("station B is more profitable");
gasStation G[] = new gasStation[10];
for(int i=0;i<10;i++) G[i] = new gasStation(3.19,3.39);
{gasStation highest =G[0];}
for (int i=1;i<10;i++)
{if (G[i].moreprofit(highest)) highest = G[i];
{System.out.println("highest total sales is" +highest.sales+ );}
//ERROR IS HERE
}
}
}
class gasStation
{
double regularprice;
double superprice;
double sales;
public gasStation(double r, double s)
{regularprice = r; superprice = s; sales = 0;}
public void sellregular(double gallons)
{sales += regularprice * gallons;}
public void sellsuper(double gallons)
{sales += superprice * gallons;}
public void gouge()
{superprice *= 2; regularprice *=2;}
public boolean moreprofit(gasStation other)
{return sales > other.sales;}
}
答案 0 :(得分:0)
更改此
for(int i=0;i<10;i++) G[i] = new gasStation(3.19,3.39);
{gasStation highest =G[0];}
到这个
for(int i=0;i<10;i++){
G[i] = new gasStation(3.19,3.39);
gasStation highest =G[0];
}
为了提高代码可读性,你应该考虑每行坚持一个语句。
编辑:
for (int i=1;i<10;i++)
{if (G[i].moreprofit(highest)) highest = G[i];
{System.out.println("highest total sales is" +highest.sales+ );}//ERROR IS HERE
}
}
将其更改为:
for (int i=1;i<10;i++){
if (G[i].moreprofit(highest))
highest = G[i];
}
System.out.println("highest total sales is" +highest.sales);
没有理由为单个println语句打开花括号。
答案 1 :(得分:0)
了解Java编码标准。你不是在追随他们。它使您的代码难以阅读。
好名字很重要。更多地考虑命名类,方法和变量。您的目标应该是易于理解和可读性。
此代码编译并运行正常。
public class LIANGLAB1 {
public static void main(String[] argv) {
GasStation gasStationA = new GasStation(3.39, 3.49);
GasStation gastStationB = new GasStation(3.19, 3.39);
gasStationA.sellRegular(10);
gasStationA.sellRegular(10);
gastStationB.sellRegular(11);
gastStationB.sellRegular(12);
if (gasStationA.hasMoreProfit(gastStationB)) System.out.println("station A is more profitable");
else System.out.println("station B is more profitable");
GasStation arrayOfGasStations[] = new GasStation[10];
for (int i = 0; i < 10; i++) {
arrayOfGasStations[i] = new GasStation(3.19, 3.39);
}
GasStation highest = arrayOfGasStations[0];
for (int i = 1; i < 10; i++) {
if (arrayOfGasStations[i].hasMoreProfit(highest)) {
highest = arrayOfGasStations[i];
}
}
System.out.println("highest total sales is" + highest.sales);
}
}
class GasStation {
double regularprice;
double superprice;
double sales;
public GasStation(double r, double s) {
regularprice = r;
superprice = s;
sales = 0;
}
public void sellRegular(double gallons) {
sales += regularprice * gallons;
}
public void sellSuper(double gallons) {
sales += superprice * gallons;
}
public void gouge() {
superprice *= 2;
regularprice *= 2;
}
public boolean hasMoreProfit(GasStation other) {
return sales > other.sales;
}
}