Java 线程主要错误中的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9662045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:46:57  来源:igfitidea点击:

Exception in thread main error

javaerror-handlinglogicmain

提问by Sh0gun

Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

可能的重复:
'java.lang.NoSuchMethodError:线程“main”中的主要异常'的原因

I'm hoping this is just a simple error here, I've looked up numerous other instantces of people getting the same error message but none of their solutions really seem to apply for me. I was just wondering if you guys could help me find the error in my code. I'm not even sure if it's functional because I can't get it to run, so I suppose it could be a logic error.

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

When I try to run the following cold I am met with fatal exception error occured. Program will exit.

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

Eclipse also gives me: java.lang.NoSuchMethodError: main Exception in thread "main"

Eclipse 还给了我:java.lang.NoSuchMethodError: main Exception in thread "main"

Thank you very much for any assistance you can offer!

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

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

采纳答案by Sumit Singh

We know that to execute any java Program we should have a mainfunction. because this is a self callable by JVM. And the signature of the function must be..

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

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

but in your code it's seem like this...

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

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

so its consider as a different function , so change your main return type..

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

回答by Ashwini Raman

The signature for main method is
public static void main(String[] args).

main 方法的签名是
public static void main(String[] args)。

When you run your program the JVM will look for this method to execute. You need to have this method in your code

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

回答by Chandra Sekhar

Your program does not contain the main method, change it to

您的程序不包含 main 方法,请将其更改为

public static void main(String[] args)

If you want to return an Integer object, then define a custom mehod and call that method inside the main method and handle that returned value.

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

回答by user1263270

Java main()function doesn't have a return-statement. This line

Javamain()函数没有返回语句。这条线

public static Integer main(String[] args)

should be

应该

public static void main(String [] args)

Also since it has no return value, you should delete the returnstatement.

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

回答by Balaswamy Vaddeman

Java's main methodshould have below signature.

Java 的 main 方法应该具有以下签名。

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

回答by Java

Try to run this code and tell if it works.

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

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