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
-
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 called
sort
.What you want to do is that you can get it right away by searching with java, array, and sort.
Related articles
- java - i intended to enter it according to the code, but it does not follow the execution result
- java - bootstrap display settings do not work
- javascript - i want to display the dialog and if it is canceled, i want to be able to enter it again
- java - about screen display in struts2
- java - i want to display the url string on the screen with jsp
- java - is it possible to start the application and display the specified url site in the image view in the screen [activity]?
- java - [android] receive and display firestore updates in real time
- java - [jsp/servlet/sql] the result display from db cannot be displayed well
- java - randomly generate integer values in the range of ± 5 integer values read from the keyboard
- java - i want to display the processed image on the screen
- i want to receive and display a java vlcj rtp live stream
- java - how to display error message when inputting non-integer
- i want to display the difference in date as "0 day 0 hour 0 minute" in java
- java - i want to display the characters entered in edittext in the same way even when the application is closed
- java - i want to display the input screen and the output result on the same screen
- java - i want to get the entire list from the db and display it, but an error occurs when executing the application
- java - i want to change the height of the red part of the screen display after the search results
- java - dictionary sort (sort combinations of numbers in descending order)
- java - i want to display an image
- java - i can't enter the number to return to the interface
Trends
So sorting is already done?
I don't need the ability to choose ascending/descending order ", but then
Stop the display
I choose descending order
You just needHere,
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.
Execution result