Java程序 编写一个应用程序求100以内的全部素数按照下面格式输出 2 3|5|7111|13|17119........ 输入说明: 无输入 输出说明: 每个数字用|线隔开,如:2|3|5|7|11||13|17|19。
时间: 2023-12-27 14:02:24 浏览: 85
以下是一个Java程序,用于求解100以内的全部素数,并按照指定格式输出:
```java
public class PrimeNumbers {
public static void main(String[] args) {
StringBuilder result = new StringBuilder();
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
result.append(i).append("|");
}
}
result.deleteCharAt(result.length() - 1);
System.out.println(result.toString());
}
}
```
相关问题
Java编写一个应用程序求100以内的全部素数按照下面格式输出 2 3|5|7111|13|17119........ 输入说明: 无输入 输出说明: 每个数字用|线隔开,如:2|3|5|7|11||13|17|19。
以下是Java编写一个应用程序求100以内的全部素数的代码:
```java
public class PrimeNumbers {
public static void main(String[] args) {
String primeNumbers = "";
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNumbers += i + "|";
}
}
System.out.println(primeNumbers);
}
}
```
输出结果为:2|3|5|7|11|13|17|19|23|29|31|37|41|43|47|53|59|61|67|71|73|79|83|89|97|
用java编写一个应用程序求100以内的全部素数按照下面格式输出 2 3|5|7111|13|17119........ 输入说明: 无输入 输出说明: 每个数字用|线隔开,如:2|3|5|7|11||13|17|19。
以下是用Java编写的求100以内全部素数的应用程序:
```java
public class PrimeNumbers {
public static void main(String[] args) {
System.out.print("2"); // 2是素数,直接输出
for (int i = 3; i <= 100; i++) {
boolean isPrime = true; // 假设i是素数
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false; // i能被j整除,不是素数
break;
}
}
if (isPrime) {
System.out.print("|" + i); // 输出素数并用|线隔开
}
}
}
}
```
输出结果为:2|3|5|7|11|13|17|19|23|29|31|37|41|43|47|53|59|61|67|71|73|79|83|89|97
阅读全文
相关推荐
















