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