7
votes

Commande de numéro ascendante et descendante en Java

Je fais un numéro d'ordre croissant et décroissant en Java et voici mon code:

Enter How Many Inputs: 5
Enter Value #1:3
Enter Value #2:5
Enter Value #3:6
Enter Value #4:11
Enter Value #5:2
Numbers in Ascending Order: 2 3 5 6 11 
Numbers in Descending Order: 


2 commentaires

Inverser vous ascendant le code permettrait de descendre .: P


Pourquoi triez-vous arr sur chaque itération? Vous n'avez besoin que de trier une fois.


13 Réponses :


0
votes

Trier le tableau juste comme avant, mais imprimez les éléments dans l'ordre inverse, en utilisant une boucle qui compte plutôt que de compter plutôt que de compter.

Aussi, déplacez le tri de la boucle - vous triez actuellement le tableau et encore une fois lorsque vous n'avez besoin que de trier une fois. xxx


0 commentaires

0
votes

Trier simplement la matrice dans l'ordre croissant et l'imprimer en arrière.

Arrays.sort(arr);
for(int i = arr.length-1; i >= 0 ; i--) {
    //print arr[i]
}


1 commentaires

@Wewdiocampo Vous pouvez récompenser votre réponse préférée en appuyant sur la coche verte sur le côté de celui-ci.



0
votes

Vous pouvez d'abord trier le tableau, puis la boucle deux fois, une fois dans les deux directions: xxx


0 commentaires

0
votes
Arrays.sort(arr, Collections.reverseOrder());
for(int i = 0; i < arr.length; i++){
    System.out.print( " " +arr[i]);
}
And move Arrays.sort() out of that for loop.. You are sorting the same array on each iteration..

0 commentaires

0
votes

Vous pouvez prendre la matrice ascendante et la sortie dans l'ordre inverse, remplacez donc la seconde pour la déclaration avec: xxx

Si vous avez Apache's Commons-Lang sur la classe de classe, il a une méthode arrayutils .Reverse (int []) que vous pouvez utiliser.

Au fait, vous ne voulez probablement pas le trier dans chaque cycle de la boucle pour la boucle.


0 commentaires

7
votes

Trois solutions possibles sont à mon esprit:

1. Inverser la commande: xxx

2. Itétrez en arrière et imprimez-le: xxx

3. Triez-le avec un comparateur "oposite": xxx


0 commentaires

1
votes

Pourquoi utilisez-vous TRAY et dérangez avec la première question du nombre de numéros recherchés?

Préférez une arracheListe associée à un comparateur correspondant: xxx


0 commentaires

1
votes

Vous pouvez effectuer deux fonctions pour ascendance et une autre pour descendre les deux fonctions suivantes Travailler après la convertir une matrice sur la liste xxx

fonction suivante xxx


0 commentaires

3
votes
public static void main(String[] args) {
          Scanner input =new Scanner(System.in);
          System.out.print("enter how many:");
         int num =input.nextInt();
    int[] arr= new int [num];
    for(int b=0;b<arr.length;b++){
   System.out.print("enter no." + (b+1) +"=");
   arr[b]=input.nextInt();
    }

    for (int i=0; i<arr.length;i++) {
        for (int k=i;k<arr.length;k++) {

        if(arr[i] > arr[k]) {

        int temp=arr[k];
        arr[k]=arr[i];
        arr[i]=temp;
        }
            }

    }
    System.out.println("******************\n output\t accending order");


    for (int i : arr){
        System.out.println(i);
    }
}
}

0 commentaires

1
votes
package pack2;

import java.util.Scanner;

public class group {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner data= new Scanner(System.in);
    int value[]= new int[5];
    int temp=0,i=0,j=0;
    System.out.println("Enter 5 element of array");
    for(i=0;i<5;i++)
    value[i]=data.nextInt();
     for(i=0;i<5;i++)
     {
     for(j=i;j<5;j++)
     {
      if(value[i]>value[j])
      {
       temp=value[i];
       value[i]=value[j];
       value[j]=temp;
      }
     }

     }
      System.out.println("Increasing Order:");
      for(i=0;i<5;i++)
           System.out.println(""+value[i]); 
    }

0 commentaires

1
votes
int arr[] = { 12, 13, 54, 16, 25, 8, 78 };

for (int i = 0; i < arr.length; i++) {
    Arrays.sort(arr);
    System.out.println(arr[i]);
}

0 commentaires

-2
votes

Je l'ai fait de cette manière (je suis nouveau dans Java (aussi dans la programmation))

public static void main(String[] args) {
    Scanner scan1=new Scanner(System.in);
    System.out.print("How many numbers you want to sort: ");
    int a=scan1.nextInt();

    int i,j,k=0; // i and j is used in various loops.
    int num[]=new int[a];
    int great[]= new int[a];    //This array elements will be used to store "the number of being greater."  

    Scanner scan2=new Scanner(System.in);
    System.out.println("Enter the numbers: ");

    for(i=0;i<a;i++)    
        num[i] = scan2.nextInt();

    for (i=0;i<a;i++) {
        for(j=0;j<a;j++) {
            if(num[i]>num[j])   //first time when executes this line, i=0 and j=0 and then i=0;j=1 and so on. each time it finishes second for loop the value of num[i] changes.
                k++;} 
    great[i]=k++;  //At the end of each for loop (second one) k++ contains the total of how many times a number is greater than the others.
    k=0;}  // And then, again k is forced to 0, so that it can collect (the total of how many times a number is greater) for another number.

    System.out.print("Ascending Order: ");
    for(i=0;i<a;i++)
        for(j=0;j<a;j++)
            if(great[j]==i) System.out.print(num[j]+","); //there is a fixed value for each great[j] that is, from 0 upto number of elements(input numbers).
    System.out.print("Discending Order: ");
    for(i=0;i<=a;i++)
        for(j=0;j<a;j++)
            if(great[j]==a-i) System.out.print(+num[j]+",");
}


1 commentaires

Bienvenue dans le débordement de pile. Bien que la question n'a pas été répondue par une réponse acceptée, elle a été posée en 2012 et a reçu plusieurs réponses, notamment une très complète de @ Jiri-Kremser. Si vous répondez à une telle question, vous devriez également noter pourquoi. En d'autres termes, quelles informations supplémentaires votre réponse fournit-elle. Par exemple, un code plus rapide, plus élégant, plus court, etc.



0
votes

Utilisez Reverse pour la boucle pour imprimer en ordre décroissant,

for (int i = ar.length - 1; i >= 0; i--) {
    Arrays.sort(ar);
    System.out.println(ar[i]);
}


0 commentaires