所以我编写了一个Java程序来计算前100个整数的平方和与前100个整数之和的平方之间的差异:
class project_euler6 {
public static int sum_of_squares(int start, int end) {
int total = 0;
while (start <= end) {
total += (start * start);
start++;
}
return total;
}
public static int square_of_sums(int start, int end) {
int total = 0;
while (start <= end) {
total += start;
start++;
}
total *= total;
return total;
}
public static void main(String[] args) {
int first_total = sum_of_squares(1, 100);
int second_total = square_of_sums(1, 100);
int difference = Math.abs(first_total - second_total);
System.out.println("The difference between the sum of the squares and the square of the sums of first 100 integers is " + difference);
}
}
当我运行它时,我得到了这个奇怪的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: project_euler6/java
Caused by: java.lang.ClassNotFoundException: project_euler6.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
不确定从这个开始......
任何帮助非常感谢, Mariogs
答案 0 :(得分:2)
问题不在于代码,也没有类的访问说明符(在类名之前不需要public
)。实际问题是你是如何执行它的。我假设您可能使用了java project_euler6.java
而不是java project_euler6
。
答案 1 :(得分:1)
对于java,主文件中至少需要一个公共类。因此,您必须将 project_euler6 作为公开。
public class project_euler6 {
/*** your code ***/
}
答案 2 :(得分:0)
尝试使用像eclipse这样的编辑器,只需创建新项目并复制粘贴上面的代码即可。代码看起来正确,应该编译并给出结果。