2013-06-27 124 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