我得到了“静态错误:这个类没有接受字符串[]的静态void main方法”尽管我在使用Dr.Java的代码中有“public static void main(String args[])
public static void main(String args[]) throws Exception {
// just for testing purposes
int myArray[] = {4,6,8,1,3,2,9,5,7,6,4,2,1,3,9,8,7,5};
mergeSort(myArray);
System.out.println("Sorted array is:\n");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
System.out.println();
}
上面有代码,我没有错误,但当我试图运行代码时,我会得到错误。提前谢谢。
下面是对象类之外的代码
class MergeSortNonRecursive {
static Stack<ProgramFrame> callStack;
// this implement the merge algorithm seen in class. Feel free to call it.
public static void merge(int A[], int start, int mid, int stop) {
int index1 = start;
int index2 = mid + 1;
int tmp[] = new int[A.length];
int indexTmp = start;
while (indexTmp <= stop) {
if (index1 <= mid && (index2 > stop || A[index1] <= A[index2])) {
tmp[indexTmp] = A[index1];
index1++;
} else {
if (index2 <= stop && (index1 > mid || A[index2] <= A[index1])) {
tmp[indexTmp] = A[index2];
index2++;
}
}
indexTmp++;
}
for (int i = start; i <= stop; i++) A[i] = tmp[i];
}
static void mergeSort(int A[]) {
/* WRITE YOUR CODE HERE */
//stack we use
callStack = new Stack<ProgramFrame>();
//initial program frame
ProgramFrame current = new ProgramFrame(0, A.length - 1, 1);
//put frame on stack
callStack.push(current);
//as long as our stack isn't empty...
while (!callStack.empty()) {
//as long as the top Frame contains more than one integer
while (callStack.peek().start < callStack.peek().stop) {
//must be defined before pushing or else the values mess up
int left = callStack.peek().start;
int middle = (callStack.peek().start + callStack.peek().stop) / 2;
int right = callStack.peek().stop;
current = new ProgramFrame(middle + 1, right, callStack.peek().PC++);
callStack.push(current);
//order ensures left is always on the top
current = new ProgramFrame(left, middle, callStack.peek().PC++);
callStack.push(current);
}
int PC = callStack.peek().PC; // need to check PC's
int start = callStack.peek().start; //assign start and mid for merge
int mid = callStack.peek().stop; //assign left and middle for merge from the left frame
callStack.pop();
//required if the next Frame (the right frame) isn't at its base of 1 integer and they have to be the same PC otherwise this will run continously
if ((callStack.peek().start != callStack.peek().stop) && (PC == callStack.peek().PC)) {
//must be defined before pushing or else the values mess up
int left = callStack.peek().start;
int middle = (callStack.peek().start + callStack.peek().stop) / 2;
int right = callStack.peek().stop;
current = new ProgramFrame(middle + 1, right, callStack.peek().PC++);
callStack.push(current);
//order ensures left is always on the top
current = new ProgramFrame(left, middle, callStack.peek().PC++);
callStack.push(current);
}
int stop = callStack.peek().stop; //get stop from the right frame
merge(A, start, mid, stop); //merge
}
}
//Static Error: This class does not have a static void main method accepting String[]. ??!??
public static void main(String args[]) throws Exception {
// just for testing purposes
int myArray[] = {4,6,8,1,3,2,9,5,7,6,4,2,1,3,9,8,7,5};
mergeSort(myArray);
System.out.println("Sorted array is:\n");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
System.out.println();
}
}
整个代码如下,我的文件保存为MergeSortnonRecursive.java
import java.util.*;
import java.io.*;
class ProgramFrame {
int start;
int stop;
int PC;
public ProgramFrame(int myStart, int myStop, int myPC) {
start = myStart;
stop = myStop;
PC = myPC;
}
// returns a String describing the content of the object
public String toString() {
return "ProgramFrame: start = " + start + " stop = " + stop + " PC = " + PC;
}
}
class MergeSortNonRecursive {
static Stack<ProgramFrame> callStack;
// this implement the merge algorithm seen in class. Feel free to call it.
public static void merge(int A[], int start, int mid, int stop) {
int index1 = start;
int index2 = mid + 1;
int tmp[] = new int[A.length];
int indexTmp = start;
while (indexTmp <= stop) {
if (index1 <= mid && (index2 > stop || A[index1] <= A[index2])) {
tmp[indexTmp] = A[index1];
index1++;
} else {
if (index2 <= stop && (index1 > mid || A[index2] <= A[index1])) {
tmp[indexTmp] = A[index2];
index2++;
}
}
indexTmp++;
}
for (int i = start; i <= stop; i++) A[i] = tmp[i];
}
static void mergeSort(int A[]) {
/* WRITE YOUR CODE HERE */
//stack we use
callStack = new Stack<ProgramFrame>();
//initial program frame
ProgramFrame current = new ProgramFrame(0, A.length - 1, 1);
//put frame on stack
callStack.push(current);
//as long as our stack isn't empty...
while (!callStack.empty()) {
//as long as the top Frame contains more than one integer
while (callStack.peek().start < callStack.peek().stop) {
//must be defined before pushing or else the values mess up
int left = callStack.peek().start;
int middle = (callStack.peek().start + callStack.peek().stop) / 2;
int right = callStack.peek().stop;
current = new ProgramFrame(middle + 1, right, callStack.peek().PC++);
callStack.push(current);
//order ensures left is always on the top
current = new ProgramFrame(left, middle, callStack.peek().PC++);
callStack.push(current);
}
int PC = callStack.peek().PC; // need to check PC's
int start = callStack.peek().start; //assign start and mid for merge
int mid = callStack.peek().stop; //assign left and middle for merge from the left frame
callStack.pop();
//required if the next Frame (the right frame) isn't at its base of 1 integer and they have to be the same PC otherwise this will run continously
if ((callStack.peek().start != callStack.peek().stop) && (PC == callStack.peek().PC)) {
//must be defined before pushing or else the values mess up
int left = callStack.peek().start;
int middle = (callStack.peek().start + callStack.peek().stop) / 2;
int right = callStack.peek().stop;
current = new ProgramFrame(middle + 1, right, callStack.peek().PC++);
callStack.push(current);
//order ensures left is always on the top
current = new ProgramFrame(left, middle, callStack.peek().PC++);
callStack.push(current);
}
int stop = callStack.peek().stop; //get stop from the right frame
merge(A, start, mid, stop); //merge
}
}
//Static Error: This class does not have a static void main method accepting String[]. ??!??
public static void main(String args[]) throws Exception {
// just for testing purposes
int myArray[] = {4,6,8,1,3,2,9,5,7,6,4,2,1,3,9,8,7,5};
mergeSort(myArray);
System.out.println("Sorted array is:\n");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
System.out.println();
}
}
一种可能性(根据经验)是,在运行Dr.Java的程序之前,请确保您在屏幕左侧栏中选择了正确的java类文件。例如,我的一个应用程序由三个java类组成。如果我选择了主方法的类,那么它就可以正常工作。但是如果我选择了项目中的另一个类,那么我会得到错误消息:“静态错误:这个类没有接受String[]的静态无效主方法。”这个错误是有道理的,因为所选的类没有主方法,但是我希望Java博士在项目中查看所有的类来找到主方法。
class MergeSortNonRecursive
需要:
public class MergeSortNonRecursive
包含主方法的类必须是公共的。
更新:在Dr Java中尝试了这个,看起来Dr Java正在尝试运行ProgramFrame。我预计完整的输出是这样的:
Welcome to DrJava. Working directory is /code
> run ProgramFrame
Static Error: This class does not have a static void main method accepting String[].
将类ProgramFrame放在公共类MergeSortNonRecursive之后可以工作。然而,您的mergesort有一个问题,它将永远循环。你需要调试一下。
我的代码编译得很好,但是当我运行它时,它给我一个错误: 这是我的密码: 我知道我需要在某个地方添加“publicstaticvoidmain(String[]args)”命令,但我不确定在哪里!谢谢你能给予的任何帮助!
我在学习,我是新手,但我想知道我做什么来“运行”它。这是一个错误:
我的路由结构 应用- 我试图在dashboard.component.ts中实现这个
错误消息: 错误:表达式的开头非法 静态字符串[]str={“一”,“二”};
问题内容: 为什么我们不能在非静态内部类中使用静态方法? 如果我将内部类设为静态,则它可以工作。为什么? 问题答案: 因为内部类的实例与外部类的实例隐式关联,所以它本身不能定义任何静态方法。由于静态嵌套类无法直接引用其封闭类中定义的实例变量或方法,因此只能通过对象引用使用它们,因此在静态嵌套类中声明静态方法是安全的。