求从键盘输入两个数(构成的区间范围内的)即是素数又是回文数的个数


import java.util.*;

class Testmethod {
	public boolean testsushu(int numbers) {
		for (int i = 2; i <= numbers / 2; i++) {
			if (numbers % i == 0) {
				return false;
			}
		}
		return true;
	}

	public boolean testhuiwenshu(int numbers) {
		int x = 0;
		int code = 0;
		if (numbers < 10) {
			return true;
		}
		while (numbers > x) {
			x = x * 10 + numbers % 10;
			numbers /= 10;
			code++;
		}
		if (code % 2 != 0) {
			if (x == numbers) {
				return true;
			} else {
				return false;
			}
		} else {
			if (x / 10 == numbers) {
				return true;
			} else {
				return false;
			}
		}
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = 0;
		int b = 0;
		int count = 0;
		a = sc.nextInt();
		b = sc.nextInt();
		Testmethod test = new Testmethod();
		for (int i = a; i <= b; i++) {
			if (test.testsushu(i) && test.testhuiwenshu(i)) {
				System.out.println(i);
				count++;
			}
		}
		System.out.println(a + "~" + b + "之间共有" + count + "个符合要求!");
	}
}



运行结果如下:
在这里插入图片描述

发布了32 篇原创文章 · 获赞 1 · 访问量 2820

猜你喜欢

转载自blog.csdn.net/YOUAREHANDSOME/article/details/105012332
今日推荐