【发布时间】:2024-01-18 16:06:01
【问题描述】:
http://projecteuler.net/problem=1
嘿。我是一名高中生,试图很好地掌握编程问题,所以我访问了 Project Euler。对于问题 1,我用 Java 编写了一些可以解决问题的代码,但显然出了点问题。我可以对什么有所了解吗?
说明: 我在索引值 332 处停止所有内容,因为 Java 从 0 开始计数,而 333 * 3 是 999,低于 1,000。 Apples 是一个单独的类,代码几乎相同,尽管它计为 5。最后,我手动将两个答案加在一起,但这是不对的。我究竟做错了什么? 最后的两个总和是:
三:164838
五:97515
public class Learning {
public static void main(String[] args){
int three[] = new int[333];
int counter = 0;
three[332] = 0;
int totalthree = 0;
int threeincrementer = 1;
int grandtotal;
boolean run = true;
boolean runagain = true;
for (counter = 1; counter<=332; counter++){
three[counter] = 3 * counter;
if (!(three[332] == 0)){
System.out.println("Finished three.");
while (run == true){
totalthree = totalthree + three[threeincrementer];
threeincrementer++;
if (threeincrementer >= 332){
run = false;
System.out.println("Three final is: " + totalthree);
}
}
}
if (runagain == true){
apples ApplesObject = new apples();
ApplesObject.rerun(0);
runagain = false;
}
}
}
}
【问题讨论】:
-
使用
System.out打印出信息并减少迭代以查看计算出错的地方。这是基本的调试,在 * 上提问之前应该做什么。 -
不要使用
System.out使用该死的调试器。 -
在您的问题陈述中,您说“Java 从 0 开始计数”,但为什么会这样呢?你的计数器是从 1 计数到 332,为什么 java 会有什么不同?