【问题标题】:Exception in thread main error [duplicate]线程主错误中的异常[重复]
【发布时间】:2012-03-12 05:10:55
【问题描述】:

可能重复:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

我希望这只是一个简单的错误,我查看了许多其他人收到相同错误消息的瞬间,但他们的解决方案似乎都没有真正适用于我。我只是想知道你们是否可以帮助我找到我的代码中的错误。我什至不确定它是否正常运行,因为我无法让它运行,所以我想这可能是一个逻辑错误。

当我尝试运行以下寒冷时,我遇到了致命的异常错误。程序将会退出。

Eclipse 还给了我: java.lang.NoSuchMethodError:主要 线程“main”中的异常

非常感谢您提供的任何帮助!

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class JoPuzzle
{

public static Integer main(String[] args)
{
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the number of soliders");
    int soldiers = input.nextInt();
    System.out.println("Please enter the how many soldiers are skipped before the next death");
    int count = input.nextInt();

     List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
      for (int i = 1; i <= count; i++) {

    soldiersList.add(i);

  }



  int currentIndex = 0;

  while(soldiersList.size() > 1) {

      currentIndex = (currentIndex - 1 + count) % soldiersList.size();

      soldiersList.remove(currentIndex);

  }

  return soldiersList.get(0);

  }   //end main

}//end class

【问题讨论】:

  • 程序运行时,JVM 将检查带有签名 public static void 的 main 方法,而您的方法中不存在该方法。它是如何开始执行的?

标签: java error-handling logic main


【解决方案1】:

我们知道要执行任何 java 程序,我们都应该有一个 ma​​in 函数。因为这是 JVM 可自调用的。 并且函数的签名必须是..

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

但在你的代码中看起来像这样......

public static Integer main(String[] args){
 }

所以它被认为是一个不同的函数,所以改变你的主要返回类型..

【讨论】:

    【解决方案2】:

    main方法的签名是
    公共静态无效主(字符串 [] 参数)。

    当你运行你的程序时,JVM 会寻找这个方法来执行。您需要在代码中使用此方法

    【讨论】:

    • 哦,哇,是的,我最初通过尝试消除我返回的士兵列表.get(0) 上的错误而不是简单地打印它来更改它。谢谢,这应该很明显。我需要睡觉-_-
    【解决方案3】:

    你的程序不包含main方法,改成

    public static void main(String[] args)
    

    如果要返回一个 Integer 对象,则定义一个自定义方法并在 main 方法中调用该方法并处理该返回值。

    【讨论】:

    • 或者您可以使用状态码退出以定义程序是否成功完成(通常状态为 0)或使用 System.exit(0) 使用您喜欢的任何其他整数发生错误;跨度>
    【解决方案4】:

    Java main() 函数没有返回语句。这一行

    public static Integer main(String[] args)
    

    应该是

    public static void main(String [] args)
    

    由于它没有返回值,你应该删除return语句。

    【讨论】:

      【解决方案5】:

      Java's main method 应该有以下签名。

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

      【讨论】:

        【解决方案6】:

        尝试运行这段代码并判断它是否有效。

        import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;
        
        
        public class JoPuzzle
        {
        
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
        
            System.out.println("Please enter the number of soliders");
            int soldiers = input.nextInt();
            System.out.println("Please enter the how many soldiers are skipped before the next death");
            int count = input.nextInt();
        
             List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
              for (int i = 1; i <= count; i++) {
        
            soldiersList.add(i);
        
          }
        
        
        
          int currentIndex = 0;
        
          while(soldiersList.size() > 1) {
        
              currentIndex = (currentIndex - 1 + count) % soldiersList.size();
        
              soldiersList.remove(currentIndex);
        
          }
        
         // return soldiersList.get(0);
        
          System.out.println(soldiersList.get(0));
          }   //end main
        
        }//end class
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-11
          • 2013-12-18
          • 1970-01-01
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多