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

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

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

问题

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

package linkedList;

public class HackerRank {
    
    public class Solution {

        // 完成下面的 aVeryBigSum 函数。
        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));

        }
    }
}
英文:

So why public static void main(String[] args) I got an error. 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&lt;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));

	    }
    }
}

答案1

得分: 1

以下是您要翻译的内容:

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

public class Solution {

    // 完成 aVeryBigSum 方法。
    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));
    }
}

这样可以确保您的静态 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.

public class Solution {

	// Complete the aVeryBigSum function below.
	public long aVeryBigSum(long[] ar) {
		long a = 0;
		for (int i = 0; i &lt; 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.

答案2

得分: 0

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

    1. 使 Solution 类为静态

    public static class Solution {

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

    }

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

    public class Solution {

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

    }

英文:

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 {

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

    }

    1. Remove the static from the main method

    public class Solution {

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

    }

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:

确定