- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到“静态错误:此类没有接受 String[] 的 static void main 方法。”即使我的代码中有“public static void main(String args[])”使用Dr. Java
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();
}
}
最佳答案
class MergeSortNonRecursive
需要:
public class MergeSortNonRecursive
包含 main 方法的类必须是公共(public)的。
更新:在 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 放在公共(public)类 MergeSortNonRecursive 之后即可使其工作。 但是,您的合并排序存在问题,并且它将永远循环。您需要对其进行调试。
关于java - 为什么我得到 "Static Error: This class does not have a static void main method accepting String[]."即使我有它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478170/
我是 F# 的菜鸟,目前正在阅读 F# 3.0 中的专家。 它是我学习的第一种编译语言(我只知道用 R 编程) 在第 6 章第 117 页,我们没有太多仪式性地介绍 静态让和静态成员。我真的不明白它是
我很迷茫。我已经花几个小时广泛地复习了我的两个类(class)。没有什么是静态的,没有什么是静态引用的,但我无法摆脱这个错误。 A 类文件 (ClassA.php) privateVariable =
关于类公共(public)类声明,请看这两段代码: public class Helper { public static void CallMeganFox(string phoneNumb
我如何使用“super”关键字从父类(super class)(类“aa”)引用“a1” class aa { protected static int a1 = 2; } public class
class Perkusja { boolean talerze = true; boolean beben = true; void zagrajNaBebnie() { Sys
我试图在编译 C++ 程序时静态链接库。 g++ (GCC) 4.8.5 20150623(红帽 4.8.5-4) $ g++ -std=c++11 -I/home/jerry/Desktop/tin
$ javac TestFilter.java TestFilter.java:19: non-static variable this cannot be referenced from a sta
这个问题在这里已经有了答案: How do I create a global, mutable singleton? (7 个答案) How can you make a safe static
“覆盖”静态数组时我遇到了一个棘手的问题。我有静态数组(为简单起见),它们在不同的派生类中具有固定长度,但在编译时仍然知道所有大小。我在基类中也有一个虚函数,但我不知道如何解决在派生类中覆盖这些数组和
我刚刚在遗留代码中发现了这一点。我知道使用宏,每当使用名称时,它都会被宏的内容替换。它们最常用于为数字常量提供符号名称。我所知道的是预处理没有类型安全、范围的概念。 这样做的真正好处是什么? #def
将 Singleton 实例声明为 static 还是声明为 static final 更好? 请看下面的例子: 静态版本 public class Singleton { private s
问题: 我观察到的行为是 TypeScript 的预期行为吗? 我观察到的行为是 ECMAScript 6 的预期行为吗? 是否有一种简单的方法可以返回继承层次结构以处理每个级别的“myStatic”
在php中,访问类的方法/变量有两种方法: 1. 创建对象$object = new Class(),然后使用”->”调用:$object->attribute/functi
我尝试向 ExpandoObject 添加一个动态方法,该方法会返回属性(动态添加)给它,但它总是给我错误。 我在这里做错了吗? using System; using System.Collecti
我试图获得一个静态链接到我的程序的音频库。我用 this灵活的包。为了让它运行,我必须按照描述构建 soloud 库 here .下载后不久,我在“build”文件夹中运行了“genie --with
这是我的webpack.prod.config.js代码 const path = require('path'); const { CleanWebpackPlugin } = require('c
我想知道什么时候应该对变量和(或)方法使用静态、最终、静态最终参数。据我了解: final:类似于c++中的const参数。它基本上意味着值(或在方法中 - 返回值)不会改变。 静态:这意味着值(或方
我一直在阅读有关使用静态对象作为锁的内容,最常见的示例如下: public class MyClass1 { private static final Object lock = new Obje
在 Visual Basic 2008 中,我知道有两种不同的方法可以完成同一件事: 成员(member)级别的 Dim: Dim counter1 as integer = 0 Dim counte
static public final int i = 0; public static final int i = 0; 两者都工作正常。 为什么同样的事情可以用两种不同的风格来完成? 最佳答案 因
我是一名优秀的程序员,十分优秀!