Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a output in form "1 1 1 2 2 " but I wanted to print the numbers separated in a line by exactly one comma

What I have tried:

<pre lang="java"><pre>public class Main {

    static int a = 0;
    static int b = 0;

    public static void main(String[] args) {
        
        int n = Integer.parseInt(args[0]);
        a = Integer.parseInt(args[1]);
        b = Integer.parseInt(args[2]);
        for (int i = 0; i <= n; i++){
            System.out.print(recursiv(i));}

    }

    public static long recursiv(int n) {

        if (n >= 0 && n <= 2)
            return 1;

        else
            return a * recursiv(n - 2) + b * recursiv(n - 3);

    }

}
Posted
Updated 16-May-18 9:00am

Get started with:
Java
for (int i = 0; i <= n; i++){
    System.out.print(recursiv(i));
    System.out.print(",");
}

I let you handle the last number.
 
Share this answer
 
Comments
Member 13817762 16-May-18 15:18pm    
thank you very much , I have solved it with for- loop .
for (int i = 0; i <= n; i++){
System.out.print(recursiv(i));
if(i<n){
System.out.print(",");}
}
Patrice T 16-May-18 15:49pm    
Nice to see it
As you have a String[] as the input, why don't you just use String.join for the output?


Java
public static void main(String[] args) {
            System.out.print(String.join(",", args);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900