The are several subjects on Ternary Operator
as here:
Except that, I don't understand the syntax.
I have a method with an if
and an else
.
public static void affichage(String[] tabPersonnage, int[] tabAge, boolean[] tabSexe){
for(int i=0; i<tabPersonnage.length; i++){
System.out.println("Personnage n° " + (i+1));
System.out.println("Pseudo : " + tabPersonnage[i]);
System.out.println("Age : " + tabAge[i] + " ");
if(tabSexe[i]){
System.out.println("Sexe : Homme ");
} else {
System.out.println("Sexe : Femme ");
}
System.out.println("-------------------");
}
}
The syntax for the ternary operator is
condition ? instruction1 : instruction2
I tried this:
public static void affichage(String[] tabPersonnage, int[] tabAge, boolean[] tabSexe){
for(int i=0; i<tabPersonnage.length; i++){
System.out.println("Personnage n° " + (i+1));
System.out.println("Pseudo : " + tabPersonnage[i]);
System.out.println("Age : " + tabAge[i] + " ");
System.out.println("Sexe : " + ((tabSexe[i]) ? "Homme": "Femme")));
System.out.println("-------------------");
}
}
I have an error message => error: ';' expected