2013-06-27 54 views
1

java新手。遵循一本书来练习编码。编译基本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的非法启动);预计

我不能指出问题。如果有人能指导我或暗示我,请做。

+1

使用IDE,如[NetBeans](https://netbeans.org/),[Intellij](http://www.jetbrains.com/idea/)或[Eclipse](http:// www。 eclipse.org/downloads/)真的可以帮助避免这样的错误。我认为甚至像[Notepad ++](http://notepad-plus-plus.org/)和[JEdit](http://www.jedit.org/)这样的基本编辑器都有一些语言支持(在语法高亮方面和支架匹配) –

+0

听起来很棒的建议。现在下载。 – KKP

回答

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."); 

} 

} 
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."); 
    } 
} 
2

方法startEngine没有其右大括号,并且还有另一个备用右括号在已定义showAtts()方法内startEngine()方法的代码

2

结束。一种方法不能定义另一种方法。

这可能是由于大括号被错误地放置。纠正他们。

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."); 

    } 
} 
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."); 

} 
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