1

java新手。通过遵循一本书来练习编码。

这是我的代码:

class Motorcycle {


    //Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
    String make;
    String color;
    boolean engineState;

    void startEngine() {
        if (engineState == true)
            System.out.print("The engine is already on.");
        else {
            engineState = true;
            System.out.print("The engine is now on.");

        }

    void showAtts() {
        System.out.print("This motorcycle is a " + color + " " + make);
        if (engineState ==true)
            System.out.print("The engine is on.");
        else System.out.print("The engine is off.");

    }
}
}

编译时出现 2 个错误:

1) 表达式 2) 的非法开始;预期的

我无法确定问题所在。如果有人可以指导我或暗示我,请做。

4

7 回答 7

3

你的一个牙套放错了地方。应该:

class Motorcycle {

//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
  String make;
  String color;
  boolean engineState;

  void startEngine() {
    if (engineState == true)
      System.out.print("The engine is already on.");
    else {
      engineState = true;
      System.out.print("The engine is now on.");
    }
  }
  void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
      System.out.print("The engine is on.");
    else System.out.print("The engine is off.");
  }
}
于 2013-06-27T04:20:06.700 回答
2

方法 startEngine 没有右大括号,并且在代码末尾有另一个备用右大括号

于 2013-06-27T04:20:18.513 回答
2

您已经在方法中定义了showAtts()方法startEngine()。一个方法不能定义另一个方法。

这可能是由于牙套放置错误造成的。纠正他们。

于 2013-06-27T04:20:22.570 回答
2
class Motorcycle {

    // Three instance variables - make and color are strings. while a
    // boolean refers to TRUE OR FLASE(in this case off or on)
    String make;
    String color;
    boolean engineState;

    void startEngine() {
        if (engineState == true)
            System.out.print("The engine is already on.");
        else {
            engineState = true;
            System.out.print("The engine is now on.");

        }
    }

    void showAtts() {
        System.out.print("This motorcycle is a " + color + " " + make);
        if (engineState == true)
            System.out.print("The engine is on.");
        else
            System.out.print("The engine is off.");

    }
}
于 2013-06-27T04:20:27.210 回答
2

您正在尝试在另一个方法中定义一个方法:

void startEngine() {
   if (engineState == true)
        System.out.print("The engine is already on.");
   else {
       engineState = true;
        System.out.print("The engine is now on.");

   }

 void showAtts() {
   System.out.print("This motorcycle is a " + color + " " + make);
   if (engineState ==true)
       System.out.print("The engine is on.");
   else System.out.print("The engine is off.");

}
}

分离出方法:

void startEngine() {
   if (engineState == true)
        System.out.print("The engine is already on.");
   else {
       engineState = true;
        System.out.print("The engine is now on.");
   }
}  // forgot this paranthesis


void showAtts() {
   System.out.print("This motorcycle is a " + color + " " + make);
   if (engineState ==true)
       System.out.print("The engine is on.");
   else System.out.print("The engine is off.");

}
于 2013-06-27T04:20:47.153 回答
2
class Motorcycle {


//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
String make;
String color;
boolean engineState;

void startEngine() {
    if (engineState == true)
        System.out.print("The engine is already on.");
    else {
        engineState = true;
        System.out.print("The engine is now on.");

    }
    } //put one here

void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
        System.out.print("The engine is on.");
    else System.out.print("The engine is off.");

}
}
// }  remove this 
于 2013-06-27T04:27:01.737 回答
1

它是正确的格式,没有错误试试这个..

public class Motorcycle {

public static void main(String[] args) {
    Motorcycle s=new Motorcycle();
    s.showAtts();
    s.startEngine();
}

//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
String make;
String color;
boolean engineState;

void startEngine() {
    if (engineState == true)
        System.out.println("The engine is already on.");
    else {
        engineState = true;
        System.out.println("The engine is now on.");

    }
}
void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
        System.out.println("The engine is on.");
    else System.out.println("The engine is off.");

}

}
于 2013-06-27T04:29:51.527 回答