我正在使用普通數組製作一個幸運數字程序,以便列表中的其餘數字不會向前移動。我所遵循的模式是1,3,7,9,13,15,21,25,31,33,37,43,49,51,63,67,69,73,75,79,87,93 ,99,...欲瞭解更多信息:Lucky Numbers幸運數字程序未顯示正確答案
下面是我製作的節目:
public class LuckyNumbers {
public static void main(String[] args) {
int[] lucky = new int[101];
for (int a = 0; a < lucky.length; a++){
lucky[a] = a;
}
//goes through each element in list
for (int b = 2; b < lucky.length; b++){
//checks if number is surviving
if (lucky[b] != 0){
/* if it does survive, go through the list deleting elements
* (setting them to zero) that fall on the
* index of the multiples of the the surviving number*/
int luckyNum = lucky[b]; // so that the number doesn't change
for (int c = 1; c < lucky.length;c++){
int d = luckyNum * c;
if (d < lucky.length){
lucky[d] = 0;
continue;
}
}
}
}
for (int f = 0; f < lucky.length; f++){
if (lucky[f] != 0){
System.out.println(lucky[f]);
}
}
}
}
輸出爲1。我認爲這是一個邏輯錯誤。
是完整的輸出只是「1」或多個「1」? – t3s0
您是否嘗試過調試?您可以看到每個號碼何時被刪除。你的邏輯有幾個問題。程序流程和輸出不是令人驚訝的。因此,我擔心,你的問題不適合Stackoverflow。 –
ISC的實際情況......對Apoorva? – khandelwaldeval