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)
Bitte geben Sie Ihren Versuch, eine 'main' Methode in Ihrem Code zu enthalten hinzufügen. – rgettman
Was genau meinen Sie mit "wenn ich das am Anfang hinzufüge"? – Mark
Wo haben Sie 'public static void main (String [] args)' hinzugefügt? –