Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Java Question  RSS feed

 
mark gile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to find the median of 3 numbers. I am having trouble figuring out where certain if statements should be. I am only allowed to use if - then and else statements. Also, no || and &&. Here is what I have so far.

if (s < t)
if (f < t)
median = f;
else if (t < f)
median = f;

//(f = first, s = second, t = third)

Could you give me some guidance on how to complete this for s and t? Nested if statements are OK.

Thanks!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's going to be easier if you indent your code properly. (You may have tried but you didn't UseCodeTags (that's a link)).



Can you figure it out from there?
 
mark gile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


am I heading in the right direction?
 
mark gile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I figured it out. It seems to be working:



Look good?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m gayle wrote:
Look good?


Since there are six permutations of three numbers, I would guess that there are three cases that are not taken care of by this set of comparisons.

Henry
 
mark gile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
m gayle wrote:
Look good?


Since there are six permutations of three numbers, I would guess that there are three cases that are not taken care of by this set of comparisons.

Henry


I see what you mean. I tried it again and it wouldn't work with certain inputs. Could I get anymore help?
 
Campbell Ritchie
Marshal
Posts: 55682
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid there is a piece of equipment which is hindering you and it needs to be removed from your attention so as not to distract you. The simplest way to do that is to power down your computer.
Get a sheet of paper and a pencil and an eraser (the latter probably the most important) and write down how you would do it without a computer. Once you have that, you will probably find it easy to turn into code.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Highly agree with Campbell.

There are six possible permutations. First, you need to figure out what those six are (listing them). And second, you need to map the path to all six of them. Mapping it out on paper was how I was able to do it.... and quite frankly, I would be very impressed if someone can do it without mapping it out first (and simply coding).

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!