2014-10-23 1 views
0
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; 
    } 
} 

사용자 입력을받는 방법이 다른가요? 아니면이 방법으로 보관할 수 있습니까? 을 실행하면 사용자 입력을 계속합니다.실행할 때 내 방법이 반복되는 이유

+2

userInt()를 호출하기 때문에 –

+0

값을 한 번만 읽지 않는 이유는 무엇입니까? 또한'userInt()'를 호출 할 때마다 Scanner 인스턴스를 생성하고 있는데, 하나의'Scanner' 인스턴스 만 필요합니다. – TheLostMind

답변

5

이처럼 재 배열 :

public static void main (String[] args) { 

    //get it and save it here! 
    int userValue = userInt(); 
    System.out.println("The palindromic primes less than " + userValue + 
        " are:"); 
    for (int i = 0; i <= userValue; i++) { 
     if (isPrime(userValue) && isPalindrome(userValue)) { 
      System.out.println(i); 
     } 
    } 

}

다음이 "userInt"값을 걱정하는 모든 메소드를 업데이트합니다.

userInt()를 호출 할 때마다 코드에 명령 줄에서 새 값을 가져 오라고 말합니다.

0

이 시도 :

public static void main (String[] args) { 
    int value = userInt(); 
    System.out.println("The palindromic primes less than " + value + 
         " are:"); 
    for (int i = 0; i <= value; i++) { 
     if (isPrime() && isPalindrome()) { 
      System.out.println(i); 
     } 
    } 

} 

용어 userInt()는 사용자에게 입력을 요구하는 기능 호출이다. 확률은이 작업을 한 번하고 싶을뿐입니다. 너는 여러 번하고있어.

0

변수에 userInt()의 결과를 저장해야합니다.

int typed = userInt(); 

그리고 나서이 변수를 사용하여 userInt()을 다시 호출하는 대신 사용자가 입력 한 것을 참조하십시오.

System.out.println("The palindromic primes less than " + typed + 
         " are:"); 

for(int i = 0; i < typed; i++) ... 
0

계속 userInt()를 계속 호출합니다. 그게 문제 야.

귀하의 논리를 이해하지 못합니다. 그래서 나는 그 코드를 수정하지 않았다. 그러나 코드가 실행됩니다.

import java.util.Scanner; 
public class PalindromicPrimes { 

    public static void main (String[] args) { 
     int x = userInt(); 
     System.out.println("The palindromic primes less than " + x + 
          " are:"); 
     for (int i = 0; i <= x; i++) { 
      if (isPrime(i) && isPalindrome(i)) { 
       System.out.println(i); 
      } 
     } 

    } 

    private static boolean isPrime(int a) { 
     if (a == 2 || a == 3) { 
      return true; 
     } 
     if (a % 2 == 0) { 
      return false; 
     } 
     int sqrt = (int) Math.sqrt(a) + 1; 
     for (int i = 3; i < sqrt; i += 2) { 
      if (a % i == 0) { 
       return false; 
      } 
     } 
     return true; 
    } 

    private static boolean isPalindrome(int a) { 
     if (a < 0) 
     return false; 
    int div = 1; 
    while (a/div >= 10) { 
     div *= 10; 
    } 
    while (a != 0) { 
     int x = a; 
     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 userInteger = s.nextInt(); 
     return userInteger; 
    } 
} 

변수 및 기능에 동일한 이름을 사용하지 마십시오. userInt() 함수에서 int userInt 변수를 사용하여 스캐너에서 결과를 얻습니다. 때때로 이것은 재귀 호출 일 수 있습니다. 그걸 조심해.

관련 문제