为什么我的方法在我跑步时重复

时间:2014-10-23 16:36:44

标签: java

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;
    }
}

有不同的获取用户输入的方式吗?或者我可以保持这种方式吗? 当它运行时它只是一直提示用户输入。

4 个答案:

答案 0 :(得分: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);
        }
    }

}

然后还会更新所有关心此问题的方法&#34; userInt&#34;值。

每次调用userInt()时,都要告诉代码从命令行获取新值。

答案 1 :(得分: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()是一个提示用户输入的函数调用。赔率你只想做一次。你多次这样做。

答案 2 :(得分:0)

您应该将userInt()的结果存储在变量中。

int typed = userInt();

然后使用此变量来引用用户键入的内容,而不是再次调用userInt()

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

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

答案 3 :(得分: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来从扫描程序获取结果。这有时可能是一个递归调用。小心。