如何用这段代码使Java递归?

发布于 2024-10-29 04:52:25 字数 1998 浏览 0 评论 0原文

我有可以输出素数的代码,但该程序使用 trycatch 。你能帮我用递归来改变这个程序吗?

package file;

import javax.swing.JOptionPane;

public class Snake {

    private static int getNilai(int number, int index) {
        if (index == 1)
            return 1;
        else if (number % index == 0)
            return 1 + getNilai(number, --index);
        else
            return 0 + getNilai(number, --index);
    }

    public static boolean cekPrime(int num) {
        if (num > 1)
            return (getNilai(num, num) == 2);
        else
            return false;
    }

    public static void main(String[] args) {
        while (true) {
            try {
                int n = Integer.parseInt(JOptionPane
                .showInputDialog("Enter your number!"));
                if (n > 0) {
                    int a = 0;
                    int b = 0;
                    int p[] = new int[n * n];
                    while (b < (n * n)) {
                        if (cekPrime(a)) {
                            p[b] = a;
                            b++;
                        }
                        a++;
                    }
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            int m = ((i + 1) + (j * n)) - 1;
                            System.out.print(p[m] + "\t");
                        }
                        System.out.println();
                    }
                    break;
                } else {
                    JOptionPane.showMessageDialog(null,
                    "Sorry, your input must be higher than 0!",
                    "System Error", JOptionPane.ERROR_MESSAGE);
                }
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null,
                "You must entering number not word!", "System Error",
                JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

I have code that can make output prime number but this program using try and catch. Can you help me change this program with using recursive?

package file;

import javax.swing.JOptionPane;

public class Snake {

    private static int getNilai(int number, int index) {
        if (index == 1)
            return 1;
        else if (number % index == 0)
            return 1 + getNilai(number, --index);
        else
            return 0 + getNilai(number, --index);
    }

    public static boolean cekPrime(int num) {
        if (num > 1)
            return (getNilai(num, num) == 2);
        else
            return false;
    }

    public static void main(String[] args) {
        while (true) {
            try {
                int n = Integer.parseInt(JOptionPane
                .showInputDialog("Enter your number!"));
                if (n > 0) {
                    int a = 0;
                    int b = 0;
                    int p[] = new int[n * n];
                    while (b < (n * n)) {
                        if (cekPrime(a)) {
                            p[b] = a;
                            b++;
                        }
                        a++;
                    }
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            int m = ((i + 1) + (j * n)) - 1;
                            System.out.print(p[m] + "\t");
                        }
                        System.out.println();
                    }
                    break;
                } else {
                    JOptionPane.showMessageDialog(null,
                    "Sorry, your input must be higher than 0!",
                    "System Error", JOptionPane.ERROR_MESSAGE);
                }
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null,
                "You must entering number not word!", "System Error",
                JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-11-05 04:52:25

该代码使用 try-catch 是因为这一行,

int n = Integer.parseInt(JOptionPane.showInputDialog("Enter your number!"));

而不是因为“非递归性”。要使程序递归,请将逻辑放入方法中,而不是执行循环,而是再次调用方法本身。仅当条件为真(不为真)时才会执行调用。在这种情况下,不要再次调用该方法,而是返回计算值(或其他值)

除此之外,还有更简单的代码来检查数字是否为素数...

The code uses try-catch because of this line

int n = Integer.parseInt(JOptionPane.showInputDialog("Enter your number!"));

not because of "non-recursiveness". To make a program recursive, put the logic into a method an instead of performing a loop, invoke the method itself again. The invocation is only performed if a condition is (not) true. In this case, do not invoke the method again but return the calculated value (or something else)

Besides this there is much easier code to check a number to be prime...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文