Java

Résolu
zmandarmarwa -  
 zmandarmarwa -
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
A voir également:

3 réponses

Arya Dröttningu Messages postés 650 Statut Membre 157
 
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
zmandarmarwa
 
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
0
Arya Dröttningu Messages postés 650 Statut Membre 157
 
tu crées un fichier, tu l'ouvres et du écrit le résultat dedans puis tu fermes ton fichier (tout ça en java)
0
zmandarmarwa
 
/*
* 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() ;
}
}
0
Arya Dröttningu Messages postés 650 Statut Membre 157
 
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);
    }
}
0
zmandarmarwa
 
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
0
Arya Dröttningu Messages postés 650 Statut Membre 157
 
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);
}
}


}
0
Arya Dröttningu Messages postés 650 Statut Membre 157
 
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);
}
}
0
zmandarmarwa
 
svp monsieur je veux que l affichage de tableau pour chaque n est sur le meme ligne
aide moi
0
Arya Dröttningu Messages postés 650 Statut Membre 157
 
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);
}
}


}
0
zmandarmarwa
 
merci monsieur
0