http://projecteuler.net/problem=1Java中的Euler Challenge 1 - 我做錯了什麼?
嘿。我是一名高中生,希望能夠很好地掌握編程問題,所以我訪問了Project Euler。對於問題1,我用Java編寫了一些可以解決問題的代碼,但顯然有些問題。我能得到一些關於什麼的見解嗎?
說明: 我停止索引值爲332的所有內容,因爲Java從0開始計數,333 * 3爲999,低於1,000。蘋果是一個單獨的類,其代碼幾乎相同,儘管它的數量爲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'和反覆次數比較少看到那裏的計算出錯。這是基本的調試,以及在StackOverflow上詢問之前應該做什麼。 – Kayaman 2014-08-28 22:17:14
請勿使用'System.out'使用血腥的調試器。 – redFIVE 2014-08-28 22:24:06
在你的問題陳述中,你說'Java從0開始計數',但爲什麼是這種情況?你的計數器從1到332,爲什麼java會做任何不同的事情? – rdowell 2014-08-28 22:35:31