Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Beginning Java
Problem with Sort Method
Nick Hallloran
Greenhorn
Posts: 19
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I need to sort three numbers in ascending order. The first two are properly done but the third number that is entered always shows up as the first number...just wondering where i am going wrong
import java.util.Scanner; public class SortThreeNumbers1 { public static void main(String[] args) { double number1; double number2; double number3; //Create scanner Scanner input = new Scanner(System.in); System.out.println("Please enter the first number: "); number1 = input.nextDouble(); System.out.println("Please enter the second number: "); number2 = input.nextDouble(); System.out.println("Please enter the third number: "); number3 = input.nextDouble(); sort(number1, number2, number3); } public static void sort(double num1, double num2, double num3){ //sort the three entered numbers double firstGreatest = 0; double secondGreatest = 0; double thirdGreatest = 0; String output = ""; if (num1 > num2 && num1 > num3) { firstGreatest = num1; num1 = 0; } else if (num2 > num1 && num2 > num3){ firstGreatest = num2; num2 = 0; } else if (num3 > num1 && num3 > num2) { firstGreatest = num3; num3 = 0; } if (num1 < firstGreatest && num1 > num2 && num1 > num3) { secondGreatest = num1; num1 = 0; } if (num2 < firstGreatest && num2 > num1 && num2 > num3){ secondGreatest = num2; num2 = 0; } if (num3 < firstGreatest && num3 > num1 && num3 > num2){ secondGreatest = num3; num3 = 0; } if (num1 < secondGreatest && num1 > num2 && num1 > num3){ thirdGreatest = num1; num1 = 0; } if (num2 < secondGreatest && num2 > num1 && num2 > num3){ thirdGreatest = num2; num2 = 0; } else if (num3 < secondGreatest && num3 > num1 && num3 > num2){ thirdGreatest = num3; num3 = 0; } output += thirdGreatest + " " + secondGreatest + " " + thirdGreatest; System.out.println(output); } }
Christophe Verré
Sheriff
Posts: 14691
16
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Be careful not to fall from your chair when looking at the following part :
output += thirdGreatest + " " + secondGreatest + " " + thirdGreatest;
[My Blog]
All roads lead to JavaRanch
Nick Hallloran
Greenhorn
Posts: 19
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
haha wow! I cant believe i missed that...yea it definitely hurt falling out of my chair
Ireneusz Kordal
Ranch Hand
Posts: 423
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Error is in lines 78/79 - your output is: third + second + third.
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Triangle Perimeter HELP!!!
Number calculations
Erroneous sym type What does this mean?
Integer Object Comparision Confusion
[solved]when i run file,promt message "Class "lab4.que1" does not have a main method."why?
More...