Results 1 to 2 of 2
Thread: JAVA assignment help
- 10-15-2012, 05:35 AM #1Member
- Join Date
- Oct 2012
- Rep Power
JAVA assignment help
I'm new to Java and I have to write a program that sorts 3 names alphabetically. I didn't learn Arrays yet so, I can't use it. I wrote the program already using if-else-if statements and it's working but, in order to do this I had to basically code every single possibility. The professor said that there's a way in which you compare two of the names, find the smallest and keep it at the bottom and then compare the other two. She refused to say more and I really can't find a way. I hope someone can help.
I know that this is not a "do homework for you" forum. I already have the program compiling and working and I can provide my code. But, I just don't understand how comparing 2 names can show me the smallest one. For example, if I compared the first 2 names and found the smallest of them....well, the third one can turn out to be the smallest.
Last edited by ScarabVenom; 10-15-2012 at 05:39 AM.
- 10-15-2012, 06:07 AM #2
Re: JAVA assignment help
Consider doing this with 3 ints (8,2,5). What you can do is compare the first value to the second and the first value to the third to see if it is smallest (or largest). If it is then you need to compare the other two values to know what order they fall in using a nested if statement.
So it would something like:
if (8 > 2 && 8 > 5)
if (2 > 5)
the order is 8 2 5 (or 5 2 8 depending on ascending or descending)
the order is 8 5 2 (or 2 5 8)
else if.... (a)
(a) We now know the first value is not the largest so all you need do is compare the second and third value
(b) We now know the last value must be largest.
In both (a) and (b) you will still need to perform the comparison between the other 2 values.
- By lance215 in forum New To JavaReplies: 1Last Post: 06-15-2012, 12:11 AM
- By nve5009 in forum New To JavaReplies: 4Last Post: 04-27-2011, 06:36 AM
- By smurf67 in forum New To JavaReplies: 4Last Post: 03-26-2011, 11:25 AM
- By java_beginner1 in forum New To JavaReplies: 11Last Post: 05-20-2010, 04:00 PM
- By xtianah77 in forum New To JavaReplies: 1Last Post: 02-18-2008, 12:54 AM