gpt4 book ai didi

java - 为什么我的代码看不到它的main方法?

转载 作者:行者123 更新时间:2023-11-30 08:56:25 24 4
gpt4 key购买 nike

我的程序看不到它的主要方法。我不明白为什么?

我有一个主要方法,但在控制台中显示:“错误:无法找到或加载主类 mergesortmini.MergeSortMini”。

如果相关,我从我的另一个项目中复制了 MergeSort 类。

public class MergeSortMini {
public static void main(String[] args) {
MergeSort m = new MergeSort();
m.init().sort().printData();
}
}

class MergeSort {


int[] data;


public MergeSort init() {
data = new int[10];
data[0] = 5;
data[1] = 67;
data[2] = 0 - 12;
data[3] = 7;
data[4] = 1;
data[5] = 2;
data[6] = 99;
data[7] = 3;
data[8] = 4;
data[9] = 67;
return this;
}


public MergeSort sort() {
data = this.mergeSort(data);
return this;
}

public int printData() {
int i = 0;
while(i < data.length){
System.out.println(data[i]);
i = i + 1;
}
return data.length;
}


public int[] merge(int[] a1, int[] a2) {

int lengthOfRes = a1.length + a2.length;
int resArr[] = new int [lengthOfRes];

int a1Index = 0;
int a2Index = 0;
int resIndex = 0;

while((a1Index < a1.length) || (a2Index < a2.length))
{
if((a1Index < a1.length) && (a2Index < a2.length)){

if(a1[a1Index] < a2[a2Index]){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
} else

{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}

else if(a1Index < a1.length){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
}

else
{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}
return resArr;
}


public int[] mergeSort(int[] arr) {

if (arr.length < 2){
return arr;
}
int mid = arr.length / 2;

int leftArr[] = new int [mid];
int rightArr[];

int halfLength = arr.length;
if(isEven(halfLength))
{
rightArr = new int [mid];
}
else
{
rightArr = new int[mid + 1];
}

int resultArr[] = new int [arr.length];

int i = 0;

while(i < mid){
leftArr[i] = arr[i];
i = i + 1;
}
int j = mid;
int indexOfRight = 0;

while(j < arr.length){
rightArr[indexOfRight] = arr[j];
indexOfRight = indexOfRight + 1;
j = j + 1;
}

leftArr = mergeSort(leftArr);
rightArr = mergeSort(rightArr);

return resultArr = merge(leftArr, rightArr);
}

public boolean isEven(int number){
return (!((number - (number * 0.5) * 2) > 0)) && (!((number - (number * 0.5) * 2) < 0));
}
}

任何帮助将不胜感激!

最佳答案

确保 MergeSortMini 有包声明

package mergesortmini;

关于java - 为什么我的代码看不到它的main方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28618205/

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