[英]How to print odd numbers using recursive java with the limits = n
我是編程語言的新手。
我有以下代碼
import javax.swing.*;
public class oddTest{
public static void odd(int a){
if (a < 0){
if (a % 2 != 0){
a++;
}
}
odd(--a);
}
public static void main(String[] args){
int n = 0;
String str = JOptionPane.showInputDialog(null, "make the limits = ");
n = Integer.parseInt(str);
odd(n);
JOptionPane.showInputDialog(n);
}
}
如果我使限制為7
,輸出應為:
輸出:1,3,5,7
我重寫了你的方法如下:
import javax.swing.JOptionPane;
public class OddTest{
public static void odd(int a){
if (a > 0){
if (a % 2 != 0){
System.out.println(a);
}
odd(--a);
}
}
public static void main(String[] args){
int n = 0;
String str = JOptionPane.showInputDialog(null, "make the limits = ");
n = Integer.parseInt(str);
odd(n);
JOptionPane.showInputDialog(n);
}
}
輸出為:7,5,3,1
如果你想要一個升序輸出,你會寫如下:
import javax.swing.JOptionPane;
public class OddTest{
public static void odd(int a, int limit){
if (a <= limit){
if (a % 2 != 0){
System.out.println(a);
}
odd(++a, limit);
}
}
public static void main(String[] args){
int n = 0;
String str = JOptionPane.showInputDialog(null, "make the limits = ");
n = Integer.parseInt(str);
odd(0, n);
JOptionPane.showInputDialog(n);
}
}
輸出為:1,3,5,7
你可以像這樣使用:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class OddCalc {
public static void main(String[] args) {
List<Integer> odds = OddCalc.odd(7);
Collections.reverse(odds);
System.out.println(odds);
}
public static List<Integer> odd(int a) {
return odd(a, new ArrayList<Integer>());
}
private static List<Integer> odd(int a, List<Integer> odds) {
if( a == 0 ) {
return odds;
}
if( a % 2 != 0 ) {
odds.add(a);
}
return odd(--a, odds);
}
}
輸出將是[1, 3, 5, 7]
,您可以將結果放在JPanel
。
為什么要使用遞歸。 使用迭代非常簡單:
public static void printOddNumbers(int max) {
for (int i = 1; i <= max; i += 2)
System.out.println(i);
}
使用遞歸:
public static void main(String[] args) {
odd(7, 1);
}
public static void odd(int max, int i) {
if (i > max)
return;
if (i > 1)
System.out.print(", ");
System.out.print(i);
odd(max, i + 2);
}
import com.sun.deploy.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Main {
static List<String> odds = new ArrayList<>();
public static void main(String[] args) {
int n = 0;
String str = JOptionPane.showInputDialog(null, "make the limits = ");
n = Integer.parseInt(str);
printOdds(n);
String result = StringUtils.join(odds, ", ");
JOptionPane.showMessageDialog(null, result);
}
static void printOdds(int n) {
if (n < 1) return;
if (n%2 == 0) n--;
printOdds(n-2);
odds.add(String.valueOf(n));
}
}
您可以使用以下代碼:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Main {
static List<String> odds = new ArrayList<>();
public static void main(String[] args) {
int n = 0;
String str = JOptionPane.showInputDialog(null, "make the limits = ");
n = Integer.parseInt(str);
printOdds(n);
StringBuilder nameBuilder = new StringBuilder();
for (String oddNum : odds) {
nameBuilder.append(oddNum).append(",");
}
nameBuilder.deleteCharAt(nameBuilder.length() - 1);
JOptionPane.showMessageDialog(null, nameBuilder.toString());
}
static void printOdds(int n) {
if (n < 1) return;
if (n%2 == 0) n--;
printOdds(n-2);
odds.add(String.valueOf(n));
}
}
如果除了jdk之外你沒有任何庫。
更好地使用流:
public static void odd(int max) {
int limit = (int)Math.ceil(max/2.0);
IntStream
.iterate(1, i -> i+2)
.limit(limit)
.forEach(i -> System.out.println(i));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.