簡體   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