Home>

Input example
Integer 1 = 12
Integer 2 = 23
Integer 3 = 34
Sample output
34>23>12

I want to create a program that inputs integers 1 to 3 and displays them in descending order. The display is in the order of integer 1, integer 2, and integer 3.

public class {
 public static void main (String [] args) {
 Scanner stdIn = new Scanner (System.in);
 System.out.print ("Integer =");
 int n = stdIn.nextInt ();
 int [] num = new int [n];
 for (int i = 0;i<n;i ++) {
 System.out.print ("integer" + (i + 1) + ":");
 num [i] = stdIn.nextInt ();
 }
 for (int i = n-1;0<i;i-) {
 for (int j = 0;j<i;j ++) {
 if (num [j + 1]<num [j]) {
 int w = num [j];
 num [j] = num [j + 1];
 num [j + 1] = w;
 }
 }
 }
 System.out.print ("Please choose the output order. (1: Ascending order, 2: Descending order):");
 int kbn = stdIn.nextInt ();
 if (kbn == 1) {
 for (int i = 0;i<n;i ++) {
 System.out.print (num [i] + "");
 }
 } else if (kbn == 2) {
 for (int i = n-1;0<= i;i-) {
 System.out.print (num [i] + "" +>);
 }
 }
 }
}

I want to output only descending order without the output order selection statement in this sentence.

  • Answer # 1

    So sorting is already done?

      

    I want to output only descending order without the output order selection statement in this sentence.

    I don't need the ability to choose ascending/descending order ", but then

    Stop the display

    I choose descending order

    You just need
    System.out.print ("Please select the output order. (1: Ascending order, 2: Descending order):");
    int kbn = stdIn.nextInt ();


    Here,

    // System.out.print ("Please choose the output order. (1: Ascending order, 2: Descending order):");
    int kbn = 2;//stdIn.nextInt ();


    By doing this, you can achieve your goals without changing anything else.


    Testing code that we decided to enter using the technique above
    If you have the same number, you don't know what to do.

    public class Main {
        public static void main (String [] args) {
    // Scanner stdIn = new Scanner (System.in);
    // System.out.print ("integer =");
    // int n = stdIn.nextInt ();
    // int [] num = new int [n];
    // for (int i = 0;i");
                    }
                }
            }
        }
    }


    Execution result

    9>9>8>7>6>2>0

  • Answer # 2

    Compare the three numbers and find the code that outputs the largest number. Let's write first

  • Answer # 3

    Sorting in ascending or descending order is calledsort.

    What you want to do is that you can get it right away by searching with java, array, and sort.

Related articles