展开全部
代码你62616964757a686964616fe58685e5aeb931333332643334参考下,主要是提供一个思路给你import java.io.*;
public class PrimeTest {
//指定m和n的值
static final int m = 0;
static final int n = 100;
//指定输出的文件路径
static final String OUT_FILE_PATH = "c:\\primes.txt";
public static void main(String[] args) {
//首先得到m到n之间质数的字符串
String temp = getPrime();
//讲字符串输出到txt文件中
outToFile(temp);
}
//输出数字到txt文件的方法
private static void outToFile(String temp) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(OUT_FILE_PATH));
bw.write(temp);
} catch (IOException e) {
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
}
}
}
}
//将m 到 n 之间的质数计算出来,并每5个就换一行
private static String getPrime() {
StringBuffer sb = new StringBuffer();
int k = 1;
for (int i = m; i <= n; i++) {
if(isPrime(i)){
if(k%5 == 0){
sb.append(i+"\r\n");
}else{
sb.append(i+"\t");
}
k++;
}
}
return sb.toString();
}
//判断一个数字是否是质数
private static boolean isPrime(int a) {
boolean boo = true;
if (a
return false;
}
for (int i = 2; i <= a / 2; i++)
if (a % i == 0) {
boo = false;
break;
}
return boo;
}
}