简体   繁体   中英

How to print odd numbers using recursive java with the limits = n

I'm a newbie of programming languages.

I have the following code

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

if i make the limits is 7 , the output should be :

the output : 1, 3, 5, 7

I've re-written your method as follow:

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

The output is: 7, 5, 3, 1

If you want an ascending output, you'll write as follow:

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

The output is: 1, 3, 5, 7

You can use like this:

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

The output will be [1, 3, 5, 7] and you can put the result in your JPanel .

Why you want to use recursion. This is very simple using iteration:

public static void printOddNumbers(int max) {
    for (int i = 1; i <= max; i += 2)
        System.out.println(i);
}

Using recursion:

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

You can use below code:

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

if you dont have any library other than jdk.

Better using streams:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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