for (int i = lo; i <= hi; i++)
{
boolean isPrime = true; // each value of i starts out assuming it IS prime
// Write a loop below that divides i by 2,3,4,5 etc upto i/2
// If at any time you find a divisor that evenly divides i
// Then set isPrime to false
/* your prime checking loop HERE */
for (int j = 2; j <= hi/2; j++)
{
if (i % j == 0)
{
isPrime = false;
}
}
// DO NOT REMOVE OR MODIFY LINE BELOW
if (isPrime)
System.out.print(i + " ");
}
好的,這裏是我現在有的代碼,我假設問題在於。該程序需要一個文本輸入文件,並將第一個值設置爲lo(在我的文本演示中,lo = 3和hi = 73)。無論出於何種原因,以「prime」輸出的唯一數字從41開始並且那之後就會完全正常。我不知道爲什麼前半部分數據根本沒有被輸出。For Loop幫助,檢查素數
請記住,我必須爲此項目使用for循環,方法等目前不在「詞彙表」中。試圖保持簡單。我會很感激幫助的人。
聖地獄,無法相信我錯過了。謝謝一堆!現在很好用! – 2015-02-12 03:16:04
很高興能幫到你!確保你接受答案。 – 2015-02-12 03:17:12