英文:
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<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 < 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
你不能在非静态类中访问静态方法。对于这个问题有两种可能的解决方案:
-
- 使 Solution 类为静态
public static class Solution {
public static void main(String[] args) { //... }
}
-
- 从 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:
-
- Make Solution static
public static class Solution {
public static void main(String[] args) { //... }
}
-
- Remove the static from the main method
public class Solution {
public void main(String[] args) { //... }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论