繁体   English   中英

为什么我运行时重复我的方法

[英]Why is my method repeating when I run

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

有没有其他方法来获取用户输入? 还是可以这样保存? 当它运行时,只会不断提示用户输入。

像这样重新排列它:

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()时,您都在告诉代码从命令行获取新值。

尝试这个:

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()是提示用户输入的函数调用。 奇怪的是您只想这样做一次。 您要做多次。

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

int typed = userInt();

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

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

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

您继续调用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,以从扫描仪获取结果。 有时这可能是一个递归调用。 小心点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM