2016-04-28 14 views
-2

Hallo Ich teste diesen Code aus für eines meiner Projekte:Java-Programm fragt nach Hauptmethode, aber es wird nicht funktionieren?

import java.util.*; 

public class Multiply 

{ 
    Random randomNumbers = new Random(); 

    int answer; // the correct answer 

    // ask the user to answer multiplication problems 
    public void quiz() 
    { 
     Scanner input = new Scanner(System.in); 

     int guess; // the user's guess 

     createQuestion(); // display the first question 

     System.out.println("Enter your answer (-1 to exit):"); 
     guess = input.nextInt(); 

     while (guess != -1) 
     { 
     checkResponse(guess); 

     System.out.println("Enter your answer (-1 to exit):"); 
     guess = input.nextInt(); 
     } // end while 
    } // end method 

    // prints a new question and stores the corresponding answer 
    public void createQuestion() 
    { 
     // get two random numbers between 0 and 9 
     int digit1 = randomNumbers.nextInt(10); 
     int digit2 = randomNumbers.nextInt(10); 

     answer = digit1 * digit2; 
     System.out.printf("How much is %d times %d?\n", 
     digit1, digit2); 
    } // end method createQuestion 

    // checks if the user answered correctly 
    public void checkResponse(int guess) 
    { 
     if (guess != answer) 
     System.out.println("No. Please try again."); 
     else 
     { 
     System.out.println("Very Good!"); 
     createQuestion(); 
     } // end else  
    } // end method checkResponse 
} // end class Multiply 
/**/ 

Und wenn ich versuche, es zu laufen, fragt es für die wichtigste Methode "public static void main (String [] args)", aber wenn ich das am Anfang hinzufüge, kommt es mit einer Menge von Fehlern. Kann mir bitte jemand sagen, wo ich falsch liege? Dies ist

wenn ich fügen Sie die Hauptmethode:

import java.util.*; 

public class Multiply 
{ 
    public static void main(String[] args)  
{ 
    Random randomNumbers = new Random(); 

    int answer; // the correct answer 

    // ask the user to answer multiplication problems 
    public void quiz() 
    { 
     Scanner input = new Scanner(System.in); 

     int guess; // the user's guess 

     createQuestion(); // display the first question 

     System.out.println("Enter your answer (-1 to exit):"); 
     guess = input.nextInt(); 

     while (guess != -1) 
     { 
     checkResponse(guess); 

     System.out.println("Enter your answer (-1 to exit):"); 
     guess = input.nextInt(); 
     } // end while 
    } // end method 

    // prints a new question and stores the corresponding answer 
    public void createQuestion() 
    { 
     // get two random numbers between 0 and 9 
     int digit1 = randomNumbers.nextInt(10); 
     int digit2 = randomNumbers.nextInt(10); 

     answer = digit1 * digit2; 
     System.out.printf("How much is %d times %d?\n", 
     digit1, digit2); 
    } // end method createQuestion 

    // checks if the user answered correctly 
    public void checkResponse(int guess) 
    { 
     if (guess != answer) 
     System.out.println("No. Please try again."); 
     else 
     { 
     System.out.println("Very Good!"); 
     createQuestion(); 
     } // end else  
    } // end method checkResponse 
} // end class Multiply 
/**/ 

Die Störung, die ich erhalte, wenn ich hinzufügen, dass:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression 
    at multiply.Multiply.main(Multiply.java:23) 
+2

Bitte geben Sie Ihren Versuch, eine 'main' Methode in Ihrem Code zu enthalten hinzufügen. – rgettman

+0

Was genau meinen Sie mit "wenn ich das am Anfang hinzufüge"? – Mark

+3

Wo haben Sie 'public static void main (String [] args)' hinzugefügt? –

Antwort

1

Es sieht aus wie Sie Funktionen innerhalb des Hauptverfahrens schaffen das könnte einige Probleme verursachen. Versuchen Sie, die Hauptmethode vom Rest der Klasse zu trennen. Zum Beispiel:

public static void main(String[] args) 
{ 
    quiz(); 
} 

-Rest of code here 
1

Sie Methoden innerhalb des Haupt-Methode definiert, wenn eine Hauptmethode die Idee Zugabe ist, dass Sie ein „main“ -Methode, von denen aufrufen, um den Rest des Programms haben. Statt alles im Inneren des Haupt Versuch des Vergebens

public static void main(String[] args){ 
// call some function here or whatnot 
} 
Verwandte Themen