Computer Science, asked by sunitazirange, 1 year ago

write a java program to arrange 3 numbers input by user in descending order

Answers

Answered by umeshkumarp742
2

Answer:

I'm currently working on a program and I just cannot get it to work the way it is supposed to work. I need to enter three integers using a scanner, and then output the numbers in ascending order. I technically got this to work but it didn't follow the structure that it HAS to follow. In my main method I have the declarations and input. Then I have to use a second method that does the sorting. I for the life of me cannot get the numbers to sort at all actually when I put the sorting inside this new method. It compiles, I run it, enter the numbers and it ends the program. Without the second method it runs correct, mostly. When it ran outside the second method there was also some errors I believe in the way that I thought was logical to sort the numbers.

Anyway this is the code that I came up with.

import java.util.Scanner;

public class Ch5PA1

{

public static void main(String[] args) {

// Declarations

Scanner input = new Scanner(System.in);

System.out.print("Enter three values: ");

int num1 = input.nextInt();

int num2 = input.nextInt();

int num3 = input.nextInt();

}

/** Sort Numbers */

public static void displaySortedNumbers(double num1, double num2, double num3){

if ((num1 < num2) && (num2 < num3)){

System.out.println("The sorted numbers are " + num1 + " " + num2 + " " + num3);

}

if ((num1 < num2) && (num2 > num3)){

System.out.println("The sorted numbers are " + num1 + " " + num3 + " " + num2);

}

if ((num1 > num2) && (num2 > num3)){

System.out.println("The sorted numbers are " + num3 + " " + num2 + " " + num1);

}

if ((num1 > num2) && (num2 < num3)){

System.out.println("The sorted numbers are " + num2 + " " + num1 + " " + num3);

}

}

}

Another thing, looking around I saw a few questions people asked about the same (or similar) issues I am having with this but the replies insist on using arrays. I cannot use an array at all for this.

Similar questions