Java中表达式的非法启动
我是Java新手。我得到一个表达式错误的非法开始。我一直在寻找答案,但无法找到,如果我使用括号不正确或没有,但我尝试了没有括号,并与他们,似乎无法通过这1错误。我需要一些帮助。 谢谢各位:Java中表达式的非法启动,java,Java,我是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
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
}
}
}
改变这个
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];
}
<> p>为了提高代码可读性,你应该考虑每行坚持一个语句。
编辑:
将其更改为:
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语句打开大括号。学习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;
}
}
最高。销售额+;}。。。删除最后一个加号。请:使用适当的缩进和换行符。它有助于可读性。拥有一个组和普遍接受的编码风格有助于可读性,不仅对你自己,尤其是对你自己!对于正在查看您的代码的其他人来说。此代码有太多错误,以至于很难知道从何处开始。自上而下都错了。这是我见过的最奇怪的括号样式。请尽量减少你的编码风格。对不起,我是个业余爱好者。
for (int i=1;i<10;i++){
if (G[i].moreprofit(highest))
highest = G[i];
}
System.out.println("highest total sales is" +highest.sales);
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;
}
}