2013-06-27 2 views
1

java를 처음 사용하였습니다. 책을 따라 코딩 연습. Heres는기본 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) 불법 시작) 예상

문제점을 지적 할 수 없습니다. 누구든지 나를지도하거나 힌트를 주시면 제발 할 수 있습니다.

+1

[NetBeans] (https://netbeans.org/), [Intellij] (http://www.jetbrains.com/idea/) 또는 [Eclipse]와 같은 IDE 사용 (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 
관련 문제