public classNumberPrinter { public static void printPrimeNumbers(int start, int end, PrimeFinder primeFinder) { int count = 0; for (int i = start; i <= end; i++) { if (primeFinder.isPrime(i)) { System.out.print(i + " "); count++; if (count == 10) { System.out.println(); count = 0; } } } } public static void main(String[] args) { PrimeFinder isPrime = n -> { if (n <= 1) { return false; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; }; printPrimeNumbers(2, 10000, isPrime); } }用Java修改
时间: 2024-02-10 12:07:58 浏览: 119
Java面试题和答案50道.docx
这段Java代码使用Lambda表达式实现了一个简单的质数打印程序,可以打印出2到10000之间的所有质数。如果您有任何问题或要求,请告诉我。
```
public class NumberPrinter {
public static void printPrimeNumbers(int start, int end, PrimeFinder primeFinder) {
int count = 0;
for (int i = start; i <= end; i++) {
if (primeFinder.isPrime(i)) {
System.out.print(i + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
public static void main(String[] args) {
PrimeFinder isPrime = n -> {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
};
printPrimeNumbers(2, 10000, isPrime);
}
}
interface PrimeFinder {
boolean isPrime(int n);
}
```
阅读全文