import java.util.Scanner;public class FindPrimes { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if(n <= 2) { System.out.println("请输入大于2的正整数"); System.exit(0); }
public class FindPrimes{ public static void print(int end){ int count=0; int j=0; for(int i=2;i<=end;i++){ // for(j=2;j<=Math.sqrt(i);j++){ // if(i%j==0){ break; } } if(j>Math.sqrt(i)){ System.out.print(i+" "); count++; if(count%10==0)System.out.println(); } } } public static void main(String args[]){ int n=100; print(n); } }
import static java.lang.Math.sqrt; import static java.lang.Math.ceil;public class FindPrimes{ public static void main(String[] args){ int count = 10; int[] primes = getPrimes(count); int n = 0; for(int prime : primes){ if(n ++ % 5 == 0){ System.out.println(); } System.out.printf("%-5d",prime); } } public static int[] getPrimes(int count){ if(count <= 0){ throw new IllegalArgumentException("count is not a valid number."); } int[] primes =new int[count]; if(count >= 1){ primes[0] = 2; } if(count >= 2){ primes[1] = 3; } int temp = 5;
outer: for(int index = 2 ; index < count ; temp += 2){ int number = (int)sqrt(temp) + 1; for(int i = 0 ; i < index && primes[i] < number; i ++){ if(temp % primes[i] == 0){ continue outer; } } primes[index ++] = temp; } return primes; } }
import java.awt.event.KeyEvent; import java.awt.event.KeyListener;import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.WindowConstants;public class ThreadTest extends JFrame implements KeyListener { private Threada ta; private Threada tb; private JTextArea area; public ThreadTest() { area = new JTextArea(); area.addKeyListener(this); this.add(area); this.setBounds(200, 200, 400, 800); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ThreadTest(); } public void keyPressed(KeyEvent key) { int code = key.getKeyCode(); System.out.println(code); if (code == 113) { // 这里设置显示的参数。第一个不用管了。 第二个参数是多少秒显示。第三个是显示内容 ta = new Threada(area, 1, "任务A执行10秒"); tb = new Threada(area, 1, "任务B执行10秒"); this.ta.start(); this.tb.start(); } if (code == 120) { this.ta.setFlag(false); this.tb.setFlag(false); } } public void keyReleased(KeyEvent key) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent key) { // TODO Auto-generated method stub }}class Threada extends Thread { private JTextArea area; private long second; private String note; private boolean flag = true; /** * 创建对象时候 设置参数 * * @param area * 文本域 * @param second * 线程 隔多少秒执行一次。单位是秒 * @param note * 在文本域中出现的内容。 */ public Threada(JTextArea area, long second, String note) { this.area = area; this.second = second; this.note = note; } @Override public void run() { while (true && flag) { try { Thread.sleep(second * 1000); } catch (InterruptedException e) { e.printStackTrace(); } this.area.append("\n" + note); } } public void setFlag(boolean flag) { this.flag = flag; } }
import static java.lang.Math.sqrt; import java.util.ArrayList;public class FindPrimes1{ public static void main(String[] args){ int max = 200; Object[] primes = getPrimes(max); int n = 0; for(Object prime : primes){ if(n ++ % 5 == 0){ System.out.println(); } System.out.printf("%-5d",(Integer)prime); } } public static Object[] getPrimes(int max){
if(max < 2){ throw new IllegalArgumentException("max is not a valid argument."); } ArrayList<Integer> list = new ArrayList<Integer>(); list.add(2); int temp = 0; int size = 0; //int count = 0 ;
outer: for(int number = 3; number <= max ; number += 2){ temp = (int)sqrt(number) + 1;
import java.util.Scanner;public class FindPrimes {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n <= 2) {
System.out.println("请输入大于2的正整数");
System.exit(0);
}
primes(n);
}
public static void primes(int n) {
int j;
for(int i=2; i<=n;i++) {
for(j=2; j<=(int)Math.sqrt(i); j++) {
if(i % j == 0) {
break;
}
}
if(j > (int)Math.sqrt(i)) {
System.out.println(i);
}
}
}
}
public class FindPrimes{
public static void print(int end){
int count=0;
int j=0;
for(int i=2;i<=end;i++){ //
for(j=2;j<=Math.sqrt(i);j++){ //
if(i%j==0){
break;
}
}
if(j>Math.sqrt(i)){
System.out.print(i+" ");
count++;
if(count%10==0)System.out.println();
}
}
}
public static void main(String args[]){
int n=100;
print(n);
}
}
import static java.lang.Math.sqrt;
import static java.lang.Math.ceil;public class FindPrimes{
public static void main(String[] args){
int count = 10;
int[] primes = getPrimes(count);
int n = 0;
for(int prime : primes){
if(n ++ % 5 == 0){
System.out.println();
}
System.out.printf("%-5d",prime);
}
} public static int[] getPrimes(int count){
if(count <= 0){
throw new IllegalArgumentException("count is not a valid number.");
}
int[] primes =new int[count]; if(count >= 1){
primes[0] = 2;
}
if(count >= 2){
primes[1] = 3;
} int temp = 5;
outer:
for(int index = 2 ; index < count ; temp += 2){
int number = (int)sqrt(temp) + 1;
for(int i = 0 ; i < index && primes[i] < number; i ++){
if(temp % primes[i] == 0){
continue outer;
}
}
primes[index ++] = temp;
} return primes;
}
}
import java.awt.event.KeyListener;import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;public class ThreadTest extends JFrame implements KeyListener {
private Threada ta; private Threada tb; private JTextArea area; public ThreadTest() {
area = new JTextArea();
area.addKeyListener(this);
this.add(area); this.setBounds(200, 200, 400, 800);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new ThreadTest(); } public void keyPressed(KeyEvent key) {
int code = key.getKeyCode();
System.out.println(code);
if (code == 113) {
// 这里设置显示的参数。第一个不用管了。 第二个参数是多少秒显示。第三个是显示内容
ta = new Threada(area, 1, "任务A执行10秒");
tb = new Threada(area, 1, "任务B执行10秒");
this.ta.start();
this.tb.start();
}
if (code == 120) {
this.ta.setFlag(false);
this.tb.setFlag(false);
}
} public void keyReleased(KeyEvent key) {
// TODO Auto-generated method stub } public void keyTyped(KeyEvent key) {
// TODO Auto-generated method stub }}class Threada extends Thread {
private JTextArea area; private long second; private String note; private boolean flag = true; /**
* 创建对象时候 设置参数
*
* @param area
* 文本域
* @param second
* 线程 隔多少秒执行一次。单位是秒
* @param note
* 在文本域中出现的内容。
*/
public Threada(JTextArea area, long second, String note) {
this.area = area;
this.second = second;
this.note = note;
} @Override
public void run() {
while (true && flag) {
try {
Thread.sleep(second * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.area.append("\n" + note);
} } public void setFlag(boolean flag) {
this.flag = flag;
}
}
import static java.lang.Math.sqrt;
import java.util.ArrayList;public class FindPrimes1{
public static void main(String[] args){
int max = 200;
Object[] primes = getPrimes(max);
int n = 0;
for(Object prime : primes){
if(n ++ % 5 == 0){
System.out.println();
}
System.out.printf("%-5d",(Integer)prime);
}
} public static Object[] getPrimes(int max){
if(max < 2){
throw new IllegalArgumentException("max is not a valid argument.");
} ArrayList<Integer> list = new ArrayList<Integer>(); list.add(2); int temp = 0; int size = 0; //int count = 0 ;
outer:
for(int number = 3; number <= max ; number += 2){ temp = (int)sqrt(number) + 1;
size = list.size(); for(int index = 0 ; index < size && temp > (Integer)list.get(index) ; index ++){
if(number % list.get(index) == 0){
//System.out.println(count ++ + "error");
continue outer;
}
} list.add(number); } return list.toArray();
}
}