import java.util.Scanner; public class Check {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
System.out.println(\"请输入一个不超过10,000,000的正整数:\"); String temp = sc.next();
StringBuffer buffer1 = new StringBuffer(temp);
StringBuffer buffer2 = new StringBuffer(temp).reverse(); buffer1 = new StringBuffer(temp);
if (buffer1.toString().equals(buffer2.toString())) { System.out.println(buffer1 + \"是回文数。\"); } else {
System.out.println(buffer1 + \"不是回文数。\"); } } }
2.从键盘读入2个正整数m和n,若m≤n,则求出在[m,n]之间的所有素数;否则求出在[n,m]之间的所有素数。
import java.util.Scanner; public class TestPrime {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(\"请输入2个正整数\"); System.out.print(\"第一个:\"); int m = sc.nextInt();
System.out.print(\"第二个:\"); int n = sc.nextInt(); if (m <= n) {
for(int i = m ;i<=n ; i++){ if(IsPrime(i)){
System.out.println(i+\" \"); } } }else{
for(int i = n; i <= m ; i++){ if(IsPrime(i)){
System.out.println(i+\" \"); } } } }
public static boolean IsPrime(int k) { if (k == 2) return true; else if (k % 2 == 0) return false; else {
for (int i = 2; i <= Math.sqrt(k); i++) if (k % i == 0) return false; return true; } }
}
2
|评论
因篇幅问题不能全部显示,请点此查看更多更全内容