Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Simple Java Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


am I heading in the right direction?
 
mark gile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think I figured it out. It seems to be working:



Look good?
 
author
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic