简体   繁体   English

为什么 eclipse 在我写 public static void main(String[] args) 的行中显示错误

[英]Why eclipse show me error in the line where i have written public static void main(String[] args)

So why public static void main(String[] args) I got an error.那么为什么public static void main(String[] args)我得到了一个错误。 what can i do to resolve it?我能做些什么来解决它?

package linkedList;

public class HackerRank {

    public class Solution {

        // Complete the aVeryBigSum function below.
        public long aVeryBigSum(long[] ar) {
            long a=0;
            for(int i=0;i<ar.length;i++){
                a=ar[i]+a;
            }

            return a;
        }


        public static void main(String[] args) { ///why this line is not correct 
            Solution s= new Solution();
            long[] ar= {10000,20000,30000};

            System.out.println(s.aVeryBigSum(ar));

        }
    }
}

There is another possible solution, taking the nested Solution class out of the HackerRank class, as I see you are not doing anything with it at the moment.还有另一种可能的解决方案,将嵌套解决方案 class 从 HackerRank class 中取出,因为我看到您目前没有对它做任何事情。

public class Solution {

    // Complete the aVeryBigSum function below.
    public long aVeryBigSum(long[] ar) {
        long a = 0;
        for (int i = 0; i < ar.length; i++) {
            a = ar[i] + a;
        }
        return a;
    }

    public static void main(String[] args) { 
        Solution s = new Solution();
        long[] ar = { 10000, 20000, 30000 };

        System.out.println(s.aVeryBigSum(ar));
    }
}

This makes sure that your static main method works.这确保您的 static 主要方法有效。

You can't access a static method in a non-static class. There are 2 possible solutions for this problem: - 1. Make Solution static您无法在非静态 class 中访问 static 方法。此问题有 2 种可能的解决方案:- 1. Make Solution static

public static class Solution {

    public static void main(String[] args) {
    //...
    }

}

- 2. Remove the static from the main method - 2.从main方法中删除static

public class Solution {

    public void main(String[] args) {
    //...
    }

}

So why public static void main(String[] args) I got an error.那么为什么public static void main(String[] args)我得到一个错误。 what can i do to resolve it?我能做些什么来解决它?

package linkedList;

public class HackerRank {

    public class Solution {

        // Complete the aVeryBigSum function below.
        public long aVeryBigSum(long[] ar) {
            long a=0;
            for(int i=0;i<ar.length;i++){
                a=ar[i]+a;
            }

            return a;
        }


        public static void main(String[] args) { ///why this line is not correct 
            Solution s= new Solution();
            long[] ar= {10000,20000,30000};

            System.out.println(s.aVeryBigSum(ar));

        }
    }
}

暂无
暂无

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

相关问题 我应该把 public static void main(String[] args) 放在哪里? - Where do I put public static void main(String[] args)? 我应该在哪里放置“公共静态void main(String [] args)”? - Where will i put “Public static void main(String[] args)”? 我在哪里添加公共 static void main(String[] args) - where do i add the public static void main(String[] args) 我必须在公共静态void main(String args [])中使用名称args吗? - Do I have to use the name args in public static void main(String args[]) “public static void main(String args[])”的Eclipse快捷方式是什么? - What is the Eclipse shortcut for “public static void main(String args[])”? public static void main(String args[]) 为什么 string args[] 是强制性的,为什么我们不使用 public static void main(Object args[])? - public static void main(String args[]) why string args[] is compulsory ,why we not use public static void main(Object args[])? Eclipse-错误:在类projectOne中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Eclipse - Error: Main method not found in class projectOne, please define the main method as: public static void main(String[] args) ECLIPSE:错误:在类apple中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - ECLIPSE : Error: Main method not found in class apple, please define the main method as: public static void main(String[] args) 了解公共静态void main(String [] args) - Understanding public static void main(String[] args) 为什么要在公共静态void main(String [] args)上引发异常? - Why should I throw an Exception on public static void main(String[] args)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM