为什么Eclipse在我编写public static void main(String[] args)的行处显示错误?

huangapple go评论98阅读模式
英文:

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

问题

所以为什么 public static void main(String[] args) 我得到了一个错误。我应该怎么做来解决它?

  1. package linkedList;
  2. public class HackerRank {
  3. public class Solution {
  4. // 完成下面的 aVeryBigSum 函数。
  5. public long aVeryBigSum(long[] ar) {
  6. long a=0;
  7. for(int i=0;i<ar.length;i++){
  8. a=ar[i]+a;
  9. }
  10. return a;
  11. }
  12. public static void main(String[] args) { /// 为什么这行是不正确的
  13. Solution s= new Solution();
  14. long[] ar= {10000,20000,30000};
  15. System.out.println(s.aVeryBigSum(ar));
  16. }
  17. }
  18. }
英文:

So why public static void main(String[] args) I got an error. what can i do to resolve it?

  1. package linkedList;
  2. public class HackerRank {
  3. public class Solution {
  4. // Complete the aVeryBigSum function below.
  5. public long aVeryBigSum(long[] ar) {
  6. long a=0;
  7. for(int i=0;i&lt;ar.length;i++){
  8. a=ar[i]+a;
  9. }
  10. return a;
  11. }
  12. public static void main(String[] args) { ///why this line is not correct
  13. Solution s= new Solution();
  14. long[] ar= {10000,20000,30000};
  15. System.out.println(s.aVeryBigSum(ar));
  16. }
  17. }
  18. }

答案1

得分: 1

以下是您要翻译的内容:

另一种可能的解决方案是,将嵌套的 Solution 类从 HackerRank 类中提取出来,因为我看到您目前没有对它进行任何操作。

  1. public class Solution {
  2. // 完成 aVeryBigSum 方法。
  3. public long aVeryBigSum(long[] ar) {
  4. long a = 0;
  5. for (int i = 0; i < ar.length; i++) {
  6. a = ar[i] + a;
  7. }
  8. return a;
  9. }
  10. public static void main(String[] args) {
  11. Solution s = new Solution();
  12. long[] ar = { 10000, 20000, 30000 };
  13. System.out.println(s.aVeryBigSum(ar));
  14. }
  15. }

这样可以确保您的静态 main 方法正常工作。

英文:

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.

  1. public class Solution {
  2. // Complete the aVeryBigSum function below.
  3. public long aVeryBigSum(long[] ar) {
  4. long a = 0;
  5. for (int i = 0; i &lt; ar.length; i++) {
  6. a = ar[i] + a;
  7. }
  8. return a;
  9. }
  10. public static void main(String[] args) {
  11. Solution s = new Solution();
  12. long[] ar = { 10000, 20000, 30000 };
  13. System.out.println(s.aVeryBigSum(ar));
  14. }
  15. }

This makes sure that your static main method works.

答案2

得分: 0

你不能在非静态类中访问静态方法。对于这个问题有两种可能的解决方案:

    1. 使 Solution 类为静态

    public static class Solution {

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

    }

    1. 从 main 方法中移除 static 关键字

    public class Solution {

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

    }

英文:

You can't access a static method in a non-static class. There are 2 possible solutions for this problem:

    1. Make Solution static

    public static class Solution {

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

    }

    1. Remove the static from the main method

    public class Solution {

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

    }

huangapple
  • 本文由 发表于 2020年4月5日 22:39:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61044317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定