Nie wystarczy użyć Console.Write
?
using System;
class MainClass {
static void recursiveOdd(int [] a, int i) {
if (a.Length == i) return;
else {
if (a[i] % 2 == 1) Console.Write(a[i]);
recursiveOdd(a, i + 1);
}
}
public static void Main (string[] args) {
int [] a = new int[] {1, 2 ,3};
recursiveOdd(a, 0); // -> 13
}
}
Edycja: Zgodnie z podpowiedzią @some_ONE , funkcja z poprawną sygnaturą:
using System;
class MainClass {
static string recursiveOdd(int [] a, int i, string s) {
if (a.Length == i) return s;
else {
if (a[i] % 2 == 1){
s += a[i].ToString();
}
return recursiveOdd(a, i + 1, s);
}
}
static string oddNumbers(int [] a) {
return recursiveOdd(a, 0, "");
}
public static void Main (string[] args) {
int [] a = new int[] {1, 2 ,3};
Console.WriteLine(oddNumbers(a));
}
}