简体   繁体   English

无法在 Intellij IDEA 中运行 Java 代码:编译时出错

[英]Unable to run the Java code in Intellij IDEA : error during Compilation

public class RotationsNumbers {
    public int main(String[] args) {
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
        return -1;
    }

    class Solution {
        int findKRotation(int arr[], int n) {
            // code here
            int ans= findPivot(arr, 0 , n-1);
            if( ans ==-1) return 0;
            return ans+1;


        }
        int findPivot( int arr[], int start, int end){
            int mid ;
            while(start< end){
                mid= start+ (end- start)/2;
                if (mid < end && arr[mid]> arr[mid+1]){
                    return mid;
                }
                if ( mid > start && arr[mid ]< arr[mid-1]){
                    return mid-1;

                }
                if ( arr[ mid ] < arr[0]){
                    end= mid-1;
                }else{
                    start= mid+1;
                }

            }
            return -1;
        }
    }
}

Edit: Tried using static void main too.. still throwing error For the above code below error is getting thrown, what should i do?编辑:尝试使用 static void main too.. 仍然抛出错误 对于上面的代码下面的错误被抛出,我该怎么办?

Error: Main method must return a value of type void in class BinarySearch.SortedArray, please define the main method as: public static void main(String[] args)错误:主方法必须在 class BinarySearch.SortedArray 中返回 void 类型的值,请将主方法定义为:public static void main(String[] args)

Process finished with exit code 1进程以退出代码 1 结束

some problems that I see我看到的一些问题

  1. like mentioned in the comment the main should be public static void main就像评论中提到的那样,主要应该是public static void main
  2. return statement in main need to go主要需要返回语句到 go

the below code should work下面的代码应该可以工作

 public static void main(String[] args) {
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(findKRotation(arr , 5));
    }

    static int findKRotation(int arr[], int n) {
        // code here
        int ans= findPivot(arr, 0 , n-1);
        if( ans ==-1) return 0;
        return ans+1;


    }
    static int findPivot( int arr[], int start, int end){
        int mid ;
        while(start< end){
            mid= start+ (end- start)/2;
            if (mid < end && arr[mid]> arr[mid+1]){
                return mid;
            }
            if ( mid > start && arr[mid ]< arr[mid-1]){
                return mid-1;

            }
            if ( arr[ mid ] < arr[0]){
                end= mid-1;
            }else{
                start= mid+1;
            }

        }
        return -1;
    }

I assume there is a reason you went with the class construction that you've gone with so I won't make any changes beyond addressing your question / concern:我认为您使用 class 结构是有原因的,所以除了解决您的问题/疑虑之外,我不会做任何更改:

Unable to run the Java code无法运行Java代码

To run your code make these changes;要运行您的代码,请进行这些更改;

Your Solution:您的解决方案:

public int main(String[] args) {
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
        return -1;
    }

Changed to:变成:

 public static void main(String[] args)
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
    }

Your Solution:您的解决方案:

class Solution {

Changed to:变成:

static class Solution {

With these minimal changes, your code should run.通过这些最小的更改,您的代码应该可以运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM