【发布时间】:2016-08-23 15:27:06
【问题描述】:
您好,我正在为我的一个项目测试此代码:
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
/**/
当我尝试运行它时,它要求主方法“public static void main(String[] args)”,但是当我在开头添加它时,它会出现大量错误。谁能告诉我哪里出错了?
这是我添加main方法的时候:
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
/**/
添加时出现的错误:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at multiply.Multiply.main(Multiply.java:23)
【问题讨论】:
-
请在您的代码中包含您尝试包含
main方法的尝试。 -
“当我在开头添加时”到底是什么意思?
-
你在哪里添加
public static void main(String[] args)? -
我在类声明之后添加它。
-
什么样的错误?