[英]My program won't run due to error with the main method/java class file exception
我试图找出选择排序算法并通过打印出每一行来理解它,但是当我运行它时,控制台甚至不会弹出。 我知道我的错误与未找到的主类异常和主字符串 args 方法有关,但我不知道如何修复它以查看输出。
package sort;
public class Sorting {
private int[] a = new int[]{ 11, 9, 17, 5, 12 };
public Sorting(int[]a) {
this.a = a;
}
public static void main(String[] args) {
}
public void sortInSpace(int[]a) {
for(int startOfUnsorted = 0; startOfUnsorted<a.length; startOfUnsorted++) {
int smallestInUnsorted = Integer.MAX_VALUE;
System.out.println(smallestInUnsorted);
}
}
}
您在 Sorting 中的 main 函数是空的,因此如果您尝试运行该类,它将不会打印任何内容。
public static void main(String[] args) {
}
相反,我会执行以下操作-
public class Sorting {
private int[] a;
public Sorting(int[]a) {
this.a = a;
}
public void sortInSpace(int[]a) {
for(int startOfUnsorted = 0; startOfUnsorted<a.length; startOfUnsorted++)
{
int smallestInUnsorted = Integer.MAX_VALUE;
System.out.println(smallestInUnsorted);
}
}
}
创建另一个类,该类将启动Sorting 对象类型并使用该对象调用该方法。
public static void main(String [] args)
{
int[] a = new int[]{ 11, 9, 17, 5, 12 };
Sorting sortObj = new Sorting(a);
sortObj.sortInSpace(a);
}
输出 -
2147483647
2147483647
2147483647
2147483647
2147483647
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.