找不到主类,Java新手

问题描述

因此,我是Java的新手,只需在创建新项目后将本课程复制并粘贴到Netbeans中即可。应该可以运行,但是不能运行,因为找不到主类。

package java.tutorial;

/**
 *
 * @author Admin
 */
public class JavaTutorial {
    
    public static int[] rotateArray(int[] arr,int k)
    {
        // Todo code application logic here
        for(int i=0;i<k%arr.length;i++)
        {
            int temp=arr[arr.length-1];
            for(int j=arr.length-1;j>0;j--){           
                arr[j]= arr[j-1];
            }
            arr[0]= temp;
        }
        return arr;
    }
}

解决方法

在这种情况下,您的应用程序需要使用main方法作为起点。

package java.tutorial;
import java.util.*;
public class JavaTutorial{
    // Include a main method and make method calls inside it.
    public static void main (String[] args){
        int[] arr = new int[]{1,2,3,4};
        System.out.println(Arrays.toString(rotateArray(arr,3)));
        // Output: [2,4,1]
    }
    public static int[] rotateArray(int[] arr,int k){
    // TODO code application logic here
        for(int i=0;i<k%arr.length;i++){
            int temp=arr[arr.length-1];
            for(int j=arr.length-1;j>0;j--){           
                arr[j]= arr[j-1];
            }
            arr[0]= temp;
        }
        return arr;
    }
}
,

任何Java项目都必须具有main方法才能启动,因此您的代码应具有main方法并在其中调用您的rotateArray

public class Solution {

    public static void main(String[] args) {
        int input[] = {1,5,6};
        int order = 3;
        int result[] = rotateArray(input,order);
    }

    public static int[] rotateArray(int[] arr,int k)
    {
        // TODO code application logic here
        for(int i=0;i<k%arr.length;i++)
        {
            int temp=arr[arr.length-1];
            for(int j=arr.length-1;j>0;j--){
                arr[j]= arr[j-1];
            }
            arr[0]= temp;
        }
        return arr;
    }
}