Help with nested conditional expression
Write an expression using nested conditional expression operators that will set the String variable heightDescription to “Tall” if the variable height is more than 6, to “Short” if height is less than 5, and to “Average” otherwise.
Code:
import java.util.Scanner;
public class QUIZ6Q4
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
System.out.println("enter height as int in feet only");
int height = scan.nextInt();
String heightDescription = ( height < 5 && height == 6 && height > 6) ? "Short" : "Average" : "Tall"; // is this considered a nested conditional expression?
System.out.println(heightDescription);
}
}
my question:
is it possible to put 3 statements in this ternary statement? i am getting a error. Syntax error on token ":".
( i use eclipse )
okay i finally figured out the syntax...
thanks for all the help... i figured it out!
Code:
Scanner scan = new Scanner (System.in);
System.out.println("enter height as int in feet only");
int height = scan.nextInt();
String heightDescription = ( height < 5) ? "Short" : (height > 6) ? "Tall" : "Average";
System.out.println(heightDescription);
it outputs the correct height...
Thanks again.