2014-09-04 138 views
-4

我是Java新手。我收到了“非法开始表达错误”。我一直在寻找答案,并找不到,如果我使用括号不正确,但我已经尝试了它没有括号,并与他们似乎并没有得到通过这1错误。我可以使用一些帮助。 谢谢:)在Java中非法启动表达式

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;} 
} 
+1

'highest.sales +);}'...删除最后的加号。并请:使用正确的缩进和换行符。它有助于可读性。拥有一个包含(和普遍接受的)编码风格有助于可读性,不仅适用于您自己,而且对于查看您的代码的其他人也尤其如此。 – 2014-09-04 19:26:09

+1

这段代码有很多错误,很难知道从哪里开始。错误从上到下。 – duffymo 2014-09-04 19:26:52

+3

这是我见过的最古怪的括号风格。 – Kayaman 2014-09-04 19:27:31

回答

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语句大括号。

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; 
    } 
}