450,419 views
22 votes
22 votes
Complete the AscendingAndDescending application so that it asks a user to enter three integers. Display them in ascending and descending order.An example of the program is shown below:Enter an integer... 1 And another... 2 And just one more... 3 Ascending: 1 2 3 Descending: 3 2 1GradingWrite your Java code in the area on the right. Use the Run button to compile and run the code. Clicking the Run Checks button will run pre-configured tests against your code to calculate a grade.Once you are happy with your results, click the Submit button to record your score.

User Chris Shaw
by
2.4k points

1 Answer

22 votes
22 votes

Answer:

Following are the solution to the given question:

import java.util.Scanner;//import package

public class AscendingAndDescending//defining a class AscendingAndDescending

{

public static void main(String[] args) //main method

{

int n1,n2,n3,min,max,m;

Scanner in = new Scanner(System.in);//creating Scanner class object

System.out.print("Enter an integer: ");//print message

n1 = in.nextInt();//input value

System.out.print("And another: ");//print message

n2 = in.nextInt();//input value

System.out.print("And just one more: ");//print message

n3 = in.nextInt();//input value

min = n1; //use min variable that holds value

max = n1; //use mix variable that holds value

if (n2 > max) max = n2;//use if to compare value and hols value in max variable

if (n3 > max) max = n3;//use if to compare value and hols value in max variable

if (n2 < min) min = n2;//use if to compare value and hols value in min variable

if (n3 < min) min = n3;//use if to compare value and hols value in min variable

m = (n1 + n2 + n3) - (min + max);//defining m variable that arrange value

System.out.println("Ascending: " + min + " " + m + " " + max);//print Ascending order value

System.out.println("Descending: " + max + " " + m + " " + min);//print Descending order value

}

}

Output:

Enter an integer: 8

And another: 9

And just one more: 7

Ascending: 7 8 9

Descending: 9 8 7

Explanation:

In this program inside the main method three integer variable "n1,n2, and n3" is declared that inputs value from the user end and also defines three variable "min, max, and m" that uses the conditional statement that checks inputs value and assigns value according to the ascending and descending order and prints its values.

User Kevinyu
by
2.6k points