Results 1 to 11 of 11
- 10-17-2010, 12:00 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
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.
my question:Java 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); } }
is it possible to put 3 statements in this ternary statement? i am getting a error. Syntax error on token ":".
( i use eclipse )
- 10-17-2010, 12:20 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
The syntax for the ternary operator is:
You have a third operand there, that's why it's producing a syntax error.Java Code:condition ? execute if true : execute if false
Also, I would consider this:
a nested statement, since it is equal to:Java Code:if(condition1 && condition2)
I don't know about your teacher though, I've seen some pretty strange things from my uni professors.Java Code:if(condition1) { if(condition2) }Last edited by m00nchile; 10-17-2010 at 12:22 AM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-17-2010, 12:22 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
No it's not considered a nested conditional ... it's considered a syntax error! As you've found ;)
Frankly nested conditionals may not be the most readable way of expressing this. But if the assignment requires it, I think it would look like:
Java Code:if(someCondition) { if(someOtherCondition) { // do something with heightDescription } else { // someCondition is true, but someOtherCondition is false // do something else with heightDescription } } else { // someCondition is false // do some third thing with heightDescription }Last edited by pbrockway2; 10-17-2010 at 12:24 AM.
- 10-17-2010, 01:19 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
I don't want 3 if statements... because they would not work for the desired outcome.
this is the question:
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.
I don't want this:
if (height < 5)
{
System.out.println("Short");
}
if ( height = 5 || height = 6 )
{
System.out.println("average");
}
etc...
i need something that satisfies the question.
- 10-17-2010, 01:28 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Well you could consider abominations along the lines of
Java Code:heightDescription = cond1 ? (cond2 ? "too hot" : "just right!") : "too cold";
(if "conditional expression operators" is a funny way about talking about ternary operators)
- 10-17-2010, 01:45 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
- 10-17-2010, 02:13 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
it would say short when should be tall and such
That's no problem, providing your conditions are sensible. Just have it print out "A", "B" and "C" then run it with some data to see what messages should go in the place of the letters.
(I realise this is a *horrible* way of figuring out the logic of the ternary operator. And that's just what makes it appropriate in this case.)
- 10-17-2010, 09:29 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
okay i finally figured out the syntax...
thanks for all the help... i figured it out!
it outputs the correct height...Java 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);
Thanks again.
- 10-17-2010, 09:32 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
how do you mark solved or something along the lines of that? I seen people tell other people to make sure to mark "solved"????
-
Click on Thread Tools at the top and then click on marked solved.
- 10-17-2010, 09:44 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
conditional statement
By blindfolded916 in forum New To JavaReplies: 12Last Post: 07-11-2010, 09:09 AM -
Lucene as Conditional Evaluator / Indexer?
By cuebei in forum LuceneReplies: 0Last Post: 01-11-2010, 07:36 PM -
JSP Help - Conditional Meta Tags
By jakavan in forum New To JavaReplies: 0Last Post: 12-08-2008, 06:34 PM -
how to do conditional looping?
By chennee72 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-09-2008, 12:38 PM -
JFrame Conditional Close
By hemanthjava in forum AWT / SwingReplies: 10Last Post: 06-29-2008, 07:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks