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);预期
我无法指出问题所在。如果有人可以指示我或暗示我,请做。
答案 0 :(得分: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.");
}
}
答案 1 :(得分:2)
方法startEngine没有关闭大括号,并且代码末尾还有另一个备用右括号
答案 2 :(得分:2)
您已在showAtts()
方法中定义了startEngine()
方法。方法不能定义另一种方法。
这可能是由于错误地放置了牙箍。纠正它们。
答案 3 :(得分: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.");
}
}
答案 4 :(得分: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.");
}
答案 5 :(得分: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
答案 6 :(得分: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.");
}
}