public class PalindromicPrimes {
public static void main (String[] args) {
userInt();
System.out.println("The palindromic primes less than " + userInt() +
" are:");
for (int i = 0; i <= userInt(); i++) {
if (isPrime() && isPalindrome()) {
System.out.println(i);
}
}
}
private static boolean isPrime() {
if (userInt() == 2 || userInt() == 3) {
return true;
}
if (userInt() % 2 == 0) {
return false;
}
int sqrt = (int) Math.sqrt(userInt()) + 1;
for (int i = 3; i < sqrt; i += 2) {
if (userInt() % i == 0) {
return false;
}
}
return true;
}
private static boolean isPalindrome() {
if (userInt() < 0)
return false;
int div = 1;
while (userInt()/div >= 10) {
div *= 10;
}
while (userInt() != 0) {
int x = userInt();
int l = x/div;
int r = x % 10;
if (l != r)
return false;
x = (x % div)/10;
div /= 100;
}
return true;
}
private static int userInt() {
Scanner s = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int userInt = s.nextInt();
return userInt;
}
}
사용자 입력을받는 방법이 다른가요? 아니면이 방법으로 보관할 수 있습니까? 을 실행하면 사용자 입력을 계속합니다.실행할 때 내 방법이 반복되는 이유
userInt()를 호출하기 때문에 –
값을 한 번만 읽지 않는 이유는 무엇입니까? 또한'userInt()'를 호출 할 때마다 Scanner 인스턴스를 생성하고 있는데, 하나의'Scanner' 인스턴스 만 필요합니다. – TheLostMind