如何用这段代码使Java递归?
我有可以输出素数的代码,但该程序使用 try
和 catch
。你能帮我用递归来改变这个程序吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码使用 try-catch 是因为这一行,
而不是因为“非递归性”。要使程序递归,请将逻辑放入方法中,而不是执行循环,而是再次调用方法本身。仅当条件为真(不为真)时才会执行调用。在这种情况下,不要再次调用该方法,而是返回计算值(或其他值)
除此之外,还有更简单的代码来检查数字是否为素数...
The code uses try-catch because of this line
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...