【问题标题】:Java program is asking for main method, but it won't work? [closed]Java程序要求main方法,但它不起作用? [关闭]
【发布时间】: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)
  • 我在类声明之后添加它。
  • 什么样的错误?

标签: java methods main


【解决方案1】:

看起来您正在 main 方法内创建函数,这可能会导致一些问题。尝试将 main 方法与类的其余部分分开。例如:

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

-Rest of code here

【讨论】:

    【解决方案2】:

    您在 main 方法中定义方法,当添加一个 main 方法时,您的想法是您有一个“main”方法来调用程序的其余部分。 与其将所有内容都放在主目录中,不如尝试添加

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

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2015-09-12
      • 2011-07-10
      • 2014-07-07
      • 1970-01-01
      • 2018-05-09
      • 2015-05-26
      • 1970-01-01
      相关资源
      最近更新 更多