Java

Résolu
Bonjour,

je veux créé une methode tri par selection en java et envoyer l 'exécution en un fichier qui est créé automatiquement en c:/ merci

3 réponses

  • Votes
  • Date
  • Date décroissante
  1. bonjour,
    déjà renseigne toi sur comment fonctionne le tri par sélection (tu devrais trouver plein d'algo sur le net).
    Ensuite il faut que tu apprennes les manipulations de fichier en java
    1. bonjour je veux le code qui me permet de mettre l execution d' une méthode dans un fichier qui est créé automatiquement en c:/ en utilisant le java
    2. tu crées un fichier, tu l'ouvres et du écrit le résultat dedans puis tu fermes ton fichier (tout ça en java)
    3. /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      package projet;
      import java.io.*;
      /**
      voici le code que je l utiliser mais il me dit apres l execution erreus

      *
      * @author Administrateur
      */
      public class bulllllllllllllllle {

      public static void main (String []args) throws IOException {
      int [] T={18,15,11,1,5,6,13,9,7,4,24,5} ;
      int n =T.length;

      int [] A =tri(T,n);
      public int[] tri(int [] T ,int taille)
      {int f;
      int inter;

      int h;
      int c=0;

      int j;

      for(h=0; h<taille; h=h+1){
      for(j=0; j<taille-h-1 ; j=j+1){
      c++;
      if(T[j]>T[j+1])
      {
      inter=T[j];
      T[j]=T[j+1];
      T[j+1]=inter;

      }
      }
      } System.out.println("le nombre de comparaison" +" " + c);
      for( f=0;f<taille;f++){
      System.out.println(T[f]);}return(T);
      }

      public static void fichier (int d) throws IOException{

      File fichier = new File("c:\\fichier.txt") ;
      PrintWriter out = new PrintWriter(new FileWriter(fichier)) ;

      out.write("Comment allez vous") ;
      out.write(d);
      out.close() ;
      }
      }
    4. En effet il y avait des erreurs mais c'est comme ça qu'on apprend ;)
      Tu ne dois pas déclarer de fonctions dans le main : il faut les déclarer juste avant.
      Voilà normalement celui là compile :

      package projet;
      
      import java.io.*;
      
      public class bulllllllllllllllle {
          public static int[] tri(int [] T ,int taille)
          {
              int inter;
              int c=0;
      
              for(int h=0; h<taille; h=h+1)
              {
                  for(int j=0; j<taille-h-1 ; j=j+1)
                  {
                      c++;
                      if(T[j]>T[j+1])
                      {
                          inter=T[j];
                          T[j]=T[j+1];
                          T[j+1]=inter;
                      }
                  }
              }
              System.out.println("le nombre de comparaison" +" " + c);
              for(int f=0;f<taille;f++)
              {
                  System.out.println(T[f]);
              }
              return T ;
          }
      
          public static void fichier (int d) throws IOException
          {
              File fichier = new File("c:\\fichier.txt") ;
              PrintWriter out = new PrintWriter(new FileWriter(fichier)) ;
              out.write("Comment allez vous") ;
              out.write(d);
              out.close() ;
          }
      
      
          public static void main (String []args) throws IOException
          {
              int [] T={18,15,11,1,5,6,13,9,7,4,24,5} ;
              int n =T.length;
              int [] A =tri(T,n);
          }
      }
      
    5. svp le programme et executabel mais il ne créé pas une fichier en c :/
      je veux qu il créé un fichier automatique et mé dans eux le retour de la fonction
      merci
  2. Essaye comme ça : par contre avant de le lancer supprime le fichier test.txt de ton ordi car là on ouvre le fichier en mode ajout ;)

    package projet;
    import java.io.*;
    
    public class NewClass1 {
    public static void fichier (int taille, int d,int an) throws IOException
    {
    File fichier = new File("C:\\test.txt") ;
    BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
    String c = String.valueOf(d);
    String h = String.valueOf(an);
    String t = String.valueOf(taille);
    out.write(t+" "+c+" "+h);
    out.newLine();
    out.close() ;
    }
    
    
    public static int[] tri(int [] T ,int taille) throws IOException
    {
        int inter;
        int c=0;
        int an =0;
        for(int h=0; h<taille; h=h+1)
        {
            for(int j=0; j<taille-h-1 ; j=j+1)
            {
                c++;
                if(T[j]>T[j+1])
                {
                    an++;
                    inter=T[j];
                    T[j]=T[j+1];
                    T[j+1]=inter;
                }
            }
        }
        System.out.println("le nombre de comparaison" +" " + c);
        for(int f=0;f<taille;f++)
        {
            System.out.println(T[f]);
        }
        fichier (taille, c,an);
        return T ;
    }
    
    
    public static void main (String []args) throws IOException
    {
    int i;
    for(i=2;i<512;i++){
        int [] T=new int [i];
        tri(T,i);
    }
    }
    
    
    }
    1. ben si tu veux que le tableau se remplisse d'éléments aléatoires faut le coder aussi !

      Seul le main change (ici on suppose des nombres entre 0 et 200) :

      public static void main (String []args) throws IOException
      {
      int i;
      int min = 0;
      int max = 200;
      for(i=2;i<512;i++){
          int [] T=new int [i];
          for (int j=0;j<T.length;j++)
              T[j] = (int)(Math.random() * (max-min)) + min;
          tri(T,i);
      }
      }
      1. svp monsieur je veux que l affichage de tableau pour chaque n est sur le meme ligne
        aide moi
      2. T'as pas trop cherché sur ce coup là :(

        package projet;
        import java.io.*;
        
        /**
        *
        * @author Administrateur
        */
        public class NewClass1 {
        public static void fichier (int taille, int d,int an) throws IOException
        {
        File fichier = new File("C:\\test.txt") ;
        BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
        String c = String.valueOf(d);
        String h = String.valueOf(an);
        String t = String.valueOf(taille);
        out.write(t+" "+c+" "+h);
        out.newLine();
        out.close() ;
        }
        
        
        public static int[] tri(int [] T ,int taille) throws IOException
        {
            int inter;
            int c=0;
            int an =0;
            for(int h=0; h<taille; h=h+1)
            {
                for(int j=0; j<taille-h-1 ; j=j+1)
                {
                    c++;
                    if(T[j]>T[j+1])
                    {
                        an++;
                        inter=T[j];
                        T[j]=T[j+1];
                        T[j+1]=inter;
                    }
                }
            }
            System.out.println("le nombre de comparaison" +" " + c);
            for(int f=0;f<taille;f++)
            {
                System.out.print(T[f]+" ");
            }
            System.out.print("\n");
            fichier (taille, c,an);
            return T ;
        }
        
        
        public static void main (String []args) throws IOException
        {
        int i;
        int min = 0;
        int max = 200;
        for(i=2;i<512;i++){
            int [] T=new int [i];
            for (int j=0;j<T.length;j++)
                T[j] = (int)(Math.random() * (max-min)) + min;
            tri(T,i);
        }
        }
        
        
        }
      3. merci monsieur