刷題————輸入一個數n,求小於等於n的全部質數

public class Text {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入一個正整數n");
		int n = sc.nextInt();
		for (int i = 2; i <= n; i++) {
			boolean flag = true;
			for (int j = 2; j <i; j++) {
				if(i%j==0){
					flag = false;
					continue;
				}
			}
			if(flag){
				System.out.print(i+" ");
			}
		}
	}

}

舉例:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章