|
-
April 1st, 2005, 05:42 PM
#4
Well, that is why I'm asking you to write something up. It will help you think through the problem and allow us to help you without just giving you an answer.
Right now the example you have given does not fit in with the project requirements since you would need the logical 'and' operator.
A simpler method may be to put the three values into an array. Then all you have to do is traverse the array and do a simple sort (I'd recommend doing a google for a java bubble sort) putting the largest values up front. Then just return the first two values.
You'd only need a simple if statement to do the actual value comparison.
Here is a simplified version of what you'd have to do
Array with three elements
// element values
[3][1][2]
Iterate through the array
temp var // to store values temporarily for swap, you may be able to get away without having this.
If arrayelement[index 1] < arrayelement[index 2]
temp var = arrayelement[index 1] // store first element so the value isn't lost during swap
arrayelement[index 1] = arrayelement[index 2] // swap the larger element to the beginning
arrayelement[index 2] = temp var // put the lower value back into the array after the larger one
The first iteration through would see that element one (3) is greater than element two (1) and leave it alone. The next comparison would see that element two (1) is less than element three (2) and swap it. Then you'd just display elements one and two.
There are plenty of examples of doing this with an array of ints out on the net. This solution is also scalable to support more than three values. =)
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|