Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Sorting With IF Statements
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ds_matrix




PostPosted: Tue Nov 06, 2007 6:46 pm   Post subject: Java Sorting With IF Statements

Im supposed to create a program that asks the user to input 3 numbers and then sorts them in ascending order from highest to lowest

My Current If Structure is:

if (num1 > num2)
{
int tstore = num1; num1 = num2; num2 = tstore;
}
else if (num1 > num2)
{
int tstore = num1; num1 = num3; num3 = tstore;
}
else if (num2 > num3)
{
int tstore = num2; num2 = num3; num3 = tstore;
}
System.out.println(num1 + "," + num2 + "," + num3);

and i tested it with n1=7, n2=99 and n3=3..and its not working and im not sure why

Any help is greatly appreciated...Thanks! Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Nov 06, 2007 6:50 pm   Post subject: RE:Java Sorting With IF Statements

well
code:

if (num1 > num2)
{ ... }
else if (num1 > num2)

seems like a problem Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ds_matrix




PostPosted: Tue Nov 06, 2007 6:51 pm   Post subject: Re: RE:Java Sorting With IF Statements

Tony @ Tue Nov 06, 2007 6:50 pm wrote:
well
code:

if (num1 > num2)
{ ... }
else if (num1 > num2)

seems like a problem Laughing


yeah...my friend helped me out with my IF statements so im not really sure how to do it myself..so im not sure what to fix..or how to fix it Sad
Clayton




PostPosted: Tue Nov 06, 2007 6:53 pm   Post subject: RE:Java Sorting With IF Statements

Look throught the logic of it. "If the first number is greater than the second, do this, if that's not true, then check to see if the first number is greater than the second number... again." Take a look through the Java documentation and wrap your head around if statements, and see if you can't figure it out Smile
ds_matrix




PostPosted: Tue Nov 06, 2007 7:00 pm   Post subject: Re: RE:Java Sorting With IF Statements

Clayton @ Tue Nov 06, 2007 6:53 pm wrote:
Look throught the logic of it. "If the first number is greater than the second, do this, if that's not true, then check to see if the first number is greater than the second number... again." Take a look through the Java documentation and wrap your head around if statements, and see if you can't figure it out Smile


Trying to figure it out...not having the greatest luck lol
HeavenAgain




PostPosted: Tue Nov 06, 2007 7:23 pm   Post subject: RE:Java Sorting With IF Statements

well, try harder

if (num1 > num2)
{ ... }
else if (num1 > num2)
{...}

whats the difference between those 2 if statement (other than they are doing different things)?
Tony




PostPosted: Tue Nov 06, 2007 7:40 pm   Post subject: Re: RE:Java Sorting With IF Statements

ds_matrix @ Tue Nov 06, 2007 6:51 pm wrote:
my friend helped me out

it could be unnecessary difficult to figure out someone else's, in this case obviously broken, code. If you don't see the problem right away, you might be better off throwing out that entire part, and starting again from scratch. It's only a few lines of code, so it's no big deal. Though since this time you'll be writing all of it yourself, you'll have a better idea of what's going on.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Barbarrosa




PostPosted: Tue Nov 06, 2007 11:30 pm   Post subject: Re: Java Sorting With IF Statements

Basically, you need to double-check your logic and your syntax. You never call a variable you haven't assigned it a value; I think it's possible you missed that. You should also look at the logic and order of things. Are you planning to do more than one thing if a condition proves true? Stick it all in the same place, if you can, we want to check it as few times as possible. You should also try using commas between variables when you assign them on the same line.

If you did your homework, the answers should come out of this.
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Wed Nov 07, 2007 8:12 pm   Post subject: Re: Java Sorting With IF Statements

just a thought but you might want to combine ur boolean logic so that its efficacy is maximized

of course do that using the && and || syntax (not sure if that is the right word here)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: