我對整個PHP業務非常陌生,並且試圖在ProjectEuler.com上完成第一項挑戰。簡單的問題:1000以下的數字可以被3和/或5整除嗎?將這些數字加在一起。添加每個數字低於20,可被3和5整除
因爲我的代碼開始噴涌出的數字令人難以置信的長串的,我只是堅持到20現在。
這是我使用的代碼:
<?php
for ($test = 1; $test <= 20; $test++) {
$total = 0;
if (($test % 3 == 0) && ($test % 5 == 0)) {
$total += $test;
}
elseif ($test % 3 == 0) {
$total += $test;
}
elseif ($test % 5 == 0) {
$total += $test;
}
}
echo $total;
?>
我覺得這是對形勢相當合乎邏輯的做法,儘管它似乎並沒有工作。我在這裏做錯了什麼?
親切的問候,桑德爾
/電子郵件:謝謝大家的幫助!
'$ total = $ total + = $ test;' - double assignment,這就是問題 – RomanPerekhrest
從語義上講,'$ total = $ total + = $ test;'有點多餘,因爲您可以將它寫爲: (1)'$ total = $ total + $ test;'或(2)'$ total + = $ test;' –
我試過了,沒有工作。 – Sander