2013-06-27 4 views
1

Nouveauté de Java. Pratiquer le codage en suivant un livre.Erreur lors de la compilation du code Java de base

Heres mon code:

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

    } 
} 
} 

Quand je compile je reçois 2 erreurs:

1) début illégale d'expression 2);

Je ne peux pas identifier le problème. Si quelqu'un peut me diriger ou me faire signe, faites-le s'il vous plaît.

+1

Utilisation d'un IDE tel que [NetBeans] (https://netbeans.org/), [Intellij] (http://www.jetbrains.com/idea/) ou [Eclipse] (http: // www. eclipse.org/downloads/) peut vraiment aider à éviter des erreurs comme celle-ci. Je pense que même certains éditeurs de base comme [Notepad ++] (http://notepad-plus-plus.org/) et [JEdit] (http://www.jedit.org/) ont un certain support linguistique (en termes de coloration syntaxique) et support correspondant) –

+0

Cela ressemble à un bon conseil. Téléchargement maintenant – KKP

Répondre

1

Il est au format correct sans erreur essayez celui-ci ..

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

L'un de vos appareils était au mauvais endroit. Devrait être:

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

la méthode startEngine n'a pas son corset bouclé de fermeture, et il y a une autre accolade fermante de rechange à la fin du code

2

Vous avez défini la méthode showAtts() méthode à l'intérieur startEngine(). Une méthode ne peut pas avoir de définition d'une autre méthode.

Cela peut être dû au mauvais positionnement des accolades. Corrige-les.

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

Vous essayez de définir une méthode dans une autre méthode:

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

} 
} 

Separate les méthodes:

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 
Questions connexes