DEV3.2 TP'S
This commit is contained in:
35
DEV/DEV3.2/TP02/Exercise2/ArrayRecursion.java
Normal file
35
DEV/DEV3.2/TP02/Exercise2/ArrayRecursion.java
Normal file
@@ -0,0 +1,35 @@
|
||||
public class ArrayRecursion {
|
||||
|
||||
public static void main(String[] args) {
|
||||
displayNumberOfPaireNumber(convertCommandArray(args));
|
||||
}
|
||||
|
||||
public static int[] convertCommandArray(String[] args) {
|
||||
return convertArray(args, 0, new int[args.length]);
|
||||
}
|
||||
|
||||
private static int[] convertArray(String[] args, int index, int[] integerArray) {
|
||||
if(index >= args.length) {
|
||||
return integerArray;
|
||||
}
|
||||
|
||||
integerArray[index] = Integer.parseInt(args[index]);
|
||||
return convertArray(args, index + 1, integerArray);
|
||||
}
|
||||
|
||||
public static void displayNumberOfPaireNumber(int[] array) {
|
||||
System.out.println("Nombre de nombres paires : " + getNumberOfPaireNumber(array, 0));
|
||||
}
|
||||
|
||||
private static int getNumberOfPaireNumber(int[] array, int index) {
|
||||
if(index >= array.length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(array[index] % 2 == 0) {
|
||||
return getNumberOfPaireNumber(array, index + 1) + 1;
|
||||
}
|
||||
|
||||
return getNumberOfPaireNumber(array, index + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user