I\'m struggling with this program here is the assignment: (Sorting three numbers
ID: 3629538 • Letter: I
Question
I'm struggling with this program here is the assignment:(Sorting three numbers) Write the following method to display an integer in reverse order:
public static void displaySortedNumbers(double num1, double num2, double num3)
Here is what I have so far:
public class SortingNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
{double num1, num2, num3;
System.out.println("Enter Three Random numbers");
System.out.print("Enter your first number: ");
num1 = input.nextDouble();
System.out.print("Enter your second number: ");
num2 = input.nextDouble();
System.out.print ("Enter your third number: ");
num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
(26.) public static void displaySortedNumbers(double num1, double num2, double num3){
double s1,s2,s3;
System.out.println("Your three numbers are: " + num1 + "" + num2 + "" + num3);
if(num1< num2){
if(num1< num3){
s1 =num1;
if(num2<num3){
s2 =num2;
s3 =num3;
}
else {
s2=num2;
s3=num3;
}
(40.) else {
if(num2<num3){
s1=num2;
if(num1<num3){
s2=num1;
s3=num3;
}
else{
s2=num3;
s3=num1;
}
}
else{
s1=num3;
s2=num2;
s3=num1;
}
(58.) System.out.println("From biggest to smallest:" +s1""+s2""+s3);
I'm getting compiler errors for the lines I labeled;
26: Illegal start of an expression
40: else without if
58: not a statement
Can anyone advise is to what changes need to be made?
Thanks!
Explanation / Answer
please rate - thanks
indent, and I prefer { on the left makes it much easier. your brackets were all messed up
import java.util.*;
public class SortingNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num1, num2, num3; //extra{
System.out.println("Enter Three Random numbers");
System.out.print("Enter your first number: ");
num1 = input.nextDouble();
System.out.print("Enter your second number: ");
num2 = input.nextDouble();
System.out.print ("Enter your third number: ");
num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
public static void displaySortedNumbers(double num1, double num2, double num3){
double s1=0,s2=0,s3=0;
System.out.println("Your three numbers are: " + num1 + "" + num2 + "" + num3);
if(num1<num2)
{if(num1<num3)
{s1=num1; //num1 is smallest
if(num2<num3)
{s2=num2;
s3=num3;
}
else
{s2=num3;
s3=num2;
}
}
else //num3 is smallest
{s1=num3;
s2=num1;
s3=num2;
}
}
else //num2 or num3 smallest
if(num2<num3)
{s1=num2; //num2 smallest
if(num1<num3)
{s2=num1;
s3=num3;
}
else
{s2=num3;
s3=num1;
}
}
else
{s1=num3;
s2=num2;
s3=num1;
}
System.out.println("From biggest to smallest:" +s3+" "+s2+" "+s1);
}
}