gpt4 book ai didi

java - 线程中的异常 "main"java.nosuchmethoderror :main

转载 作者:行者123 更新时间:2023-12-02 00:14:05 25 4
gpt4 key购买 nike

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

我是java新手,我想写一个程序来交换2个号码。
我在上面写了 2 个程序。一个正在运行,另一个没有运行。
我无法理解未运行程序的错误。请帮助我理解我的错误。
在这里,我为您提供了两个程序以及输出。

正在运行的程序:

public class SwapElementsExample {


public static void main(String[] args) {

int num1 = 10;
int num2 = 20;

System.out.println("Before Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
swap(num1, num2);
}

private static void swap(int num1, int num2) {
int temp = num1;
num1 = num2;
num2 = temp;

System.out.println("After Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
}
}

输出是:

交换之前
num1 的值为:10
num2 的值为:20
交换后
num1 的值为:20
num2 的值为:10

在上述程序中我没有任何问题。
但在下一个程序中我找不到什么错误。
请帮我找出错误。

class Swap  
{
public static void main(int a, int b)
{
int c=0;
c=b;
b=a;
a=c;
c=0;
System.out.println(a);
System.out.println(b);
}
}

执行中没有错误信息。
但在运行时有一个错误消息,即:
线程“main”中的异常 java.nosuchmethoderror:main

请告诉我这个程序的问题。

<小时/>

最佳答案

public static void main(int a, int b) 不正确。

它必须是:public static void main(String[] args)。这是根据定义。

如果你想获取第一个和第二个参数:

int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

关于java - 线程中的异常 "main"java.nosuchmethoderror :main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12210196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com