Java程序正在请求main方法,但它赢了';不行?
您好,我正在为我的一个项目测试此代码:Java程序正在请求main方法,但它赢了';不行?,java,methods,main,Java,Methods,Main,您好,我正在为我的一个项目测试此代码: 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
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
/**/
当我尝试运行它时,它会要求使用main方法“publicstaticvoidmain(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)
{
quiz();
}
-Rest of code here
您在main方法中定义方法,添加main方法时,您的想法是有一个“main”方法来调用程序的其余部分。 不要将所有内容都放在主菜单中,而是尝试添加
public static void main(String[] args){
// call some function here or whatnot
}
请在代码中包含您试图包含的
main
方法。您所说的“当我在开头添加它时”是什么意思?您在哪里添加了publicstaticvoidmain(String[]args)
?我在类声明之后添加它。什么类型的错误?