我正在尝试解决一个示例练习,该练习根据输入的范围显示质数。例如,如果我输入 10 它应该输出2 3 5 7 11 13 17 19 23 29.
这是我的代码:
System.out.print("Enter Range: ");
int range = input.nextInt();
int r = 0;
for(int ctr1 = 2; ctr1 <= range; ctr1++){
for(int ctr2 = 1; ctr2 <= ctr1; ctr2++){
if(ctr1%ctr2 == 0){
r++;
}
}
if(r == 2){
System.out.println(ctr1);
}
}
当我输入 10 它只输出 2 时会发生什么。谁能告诉我我的代码中的错误?
开心每一天1111
Smart猫小萌
相关分类