-1
import java.util.Scanner;
public class Main {
public static void main (String[] args)
{
int value;
int check = 2;
boolean flag = true;
Scanner input = new Scanner(System.in);
System.out.println("Number of times:");
value = input.nextInt();
for(int i = 2; i <= value;i++)
{
while(check <= i/2)
{
if(i%check == 0)
{
flag = false;
break;
}
check++;
}
if(flag)
{
System.out.print(i + ", ");
}
}
}
輸出總是打印:3的倍數和5個雖然環路誤差爲素數
程序打印2,3,5,7,9,11,13,15
我想知道爲什麼它會發生,因爲當我幹運行它,它不拿起3和5
沒錯如果檢查終端是sqrt而不是i/2,那麼它也將有所幫助,但這對於正確性並不需要 –