1. ホーム
  2. java

[解決済み] なぜ「Static Error: このクラスには String[] を受け入れる静的 void main メソッドがありません" と表示されるのですが?

2022-02-03 15:50:03

質問内容

Static Errorが発生しました。このクラスは、String[]を受け入れる静的ボイドメインメソッドを持っていません" "私のコードの一部として "public static void main(String args[])" を持っているにもかかわらず Dr.ジャバを使って

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[].

public class MergeSortNonRecursive の後に class ProgramFrame を置くと動作します。 しかし マージソートに問題があり、永遠にループします。デバッグが必要です。