IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Sortir 5 numéros


Sujet :

avec Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut Sortir 5 numéros
    Bonjour,
    je veux que prg donne que 5 numeros de mon array
    quelqu'un peut m'aider comment le faire svp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    import java.util.*;
    public class another 
    {
    	    public static void main(String[] arguments) 
    	    {
    	    	String number[] = { "10", "20", "30", "25",
    		            "22", "42", "50" };
     
    		        int [] order = new int[number.length];
     
    		        System.out.println("The original order:");
    		        for (int i = 0; i < number.length; i++){
    		            order[i] = i;
    		            System.out.println(i + ": " + number[i]);
    	        }
     
    	        System.out.println("The new order:");
    	        int orderLen = number.length;
    		        for (int i = 0; i < number.length; i++){
    		            int index = (int) (Math.random() * orderLen);
    		            int pos = order[index];
    		            System.out.println(pos + ": " + number[pos]);
    		            order[index] = order[--orderLen];
    		        }
    		    }
    		}

  2. #2
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Points : 13 670
    Points
    13 670
    Par défaut
    Citation Envoyé par domxaline Voir le message
    Bonjour,
    je veux que prg donne que 5 numeros de mon array
    quelqu'un peut m'aider comment le faire svp
    Il manque des étapes dans tout ça. Tu nous donnes une vague description du but de ton code et tu nous le donnes comme ça sans explication.

    Si tu veux une aide appropriée, précises nous clairement ce que doit faire ce code et indiques nous (là aussi avec précision) là où tu bloques, les symptômes du blocage et pourquoi ?

    Merci de respecter ces principes pour avoir des réponses informatives et claires.

  3. #3
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Octobre 2004
    Messages
    398
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2004
    Messages : 398
    Points : 710
    Points
    710
    Par défaut
    en répondant bêtement, je te dirais de ne prendre que les 5 premiers éléments de ton tableau, donc
    for (int i = 0; i < 5; i++)


    mais je suppose que tu veux les prendre au hasard, donc plutôt :
    initialiser une liste d'indices (tirés au hasard donc)
    tant que j'ai pas 5 indices de tableau différents dans ma liste:
    1- tirer un numéro au hasard entre 0 et number.lenght (voir Math.random)
    2- si numéro déjà tiré (donc présent ds la liste), retour à 1
    sinon ajouter l'indice tiré au hasard à la liste, retour 1

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    ma idée est
    je veux que le prg sort 5 numéros par hasard,
    parmi les numéros que j'ai dans l'array ;

    voilà c'est tout

  5. #5
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Points : 13 670
    Points
    13 670
    Par défaut
    Citation Envoyé par domxaline Voir le message
    ma idée est
    dans l'array je donnes quelques numéros
    parmi ces numéros,je veux que le prg sort 5 numéros par hasard
    voilà c'est tout
    Et donc où est-ce que ça bloque et qu'est ce qui se passe ?

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    for(int j=0; j < 5 ; j++)
    System.out.println("Number"+ (j+1) + (String)(Math.random()number));
    en écrivant comme ça j'ai un erreur
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot cast from double to String
    Syntax error on token "number", delete this token

    at another.main(another.java:28)

  7. #7
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Points : 13 670
    Points
    13 670
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    for(int j=0; j < 5 ; j++)
    System.out.println("Number"+ (j+1) + (String)(Math.random()number));
    Tu trouves cette syntaxe en rouge logique ?

    C'est étrange mais cette portion de code sort de nulle part, elle n'était pas dans l'extrait que tu nous as fournis au premier abord.

  8. #8
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    import java.util.*;
    public class another 
    {
    	    public static void main(String[] arguments) 
    	    {
    	    	String number[] = { "1","3","7","8","10","13","17","18","19","20","23","24","26",
    	    			"27","29","32","33","35","39","41","43","46","47","48","49","50" };
     
    		        int [] order = new int[number.length];
     
    		        System.out.println("The original order:");
    		        for (int i = 0; i < number.length; i++){
    		            order[i] = i;
    		            System.out.println(i + ": " + number[i]);
    	        }
     
    	        System.out.println("The new order:");
    	        int orderLen = number.length;
    		        for (int i = 0; i < number.length; i++)
    		        {
    		            int index = (int) (Math.random() * orderLen);
    		            int pos = order[index];
    		            System.out.println(pos + ": " + number[pos]);
    		            order[index] = order[--orderLen];  
     
    		        }
    		        for(int j=0; j < 5 ; j++)
    		            System.out.println("Number"+ (j+1) + (String)(Math.random()number));
     
    		    }
    		}

  9. #9
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 566
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 566
    Points : 21 632
    Points
    21 632
    Par défaut
    Hum. C'est certes des maths, mais ce n'est pas de la syntaxe mathématique, c'est de la syntaxe de programmation.

    Le symbole "multiplier" ne peut pas être omis. Pour rappel, c'est un astérisque *

  10. #10
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    bonjour
    en ajoutant *,j'ai erreur message
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator * is undefined for the argument type(s) double, String[]

    at another.main(another.java:29)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     for(int j=0; j < 5 ; j++)
     System.out.println("Number"+ (j+1) + (int)(Math.random()*number));

  11. #11
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Points : 13 670
    Points
    13 670
    Par défaut
    Citation Envoyé par domxaline Voir le message
    bonjour
    en ajoutant *,j'ai erreur message
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator * is undefined for the argument type(s) double, String[]

    at another.main(another.java:29)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     for(int j=0; j < 5 ; j++)
     System.out.println("Number"+ (j+1) + (int)(Math.random()*number));
    Je crois que tu ne saisis pas du tout ce que tu fais là.

    Tu as déclaré un tableau de String qui se nomme "number". Et là tu fais donc une multiplication entre un nombre aléatoire donné par "Math.random() et un tableau de String (ce qu'évidemment ton compilateur n'acceptera pas). Pourquoi créer un tableau de String alors que tu veux faire un tableau de numéros ? Et pourquoi faire une multiplication entre un nombre aléatoire et un tableau ?

  12. #12
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Octobre 2004
    Messages
    398
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2004
    Messages : 398
    Points : 710
    Points
    710
    Par défaut
    Citation Envoyé par domxaline Voir le message
    bonjour
    en ajoutant *,j'ai erreur message
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator * is undefined for the argument type(s) double, String[]

    at another.main(another.java:29)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     for(int j=0; j < 5 ; j++)
     System.out.println("Number"+ (j+1) + (int)(Math.random()*number));
    int min = 0;
    int max = 5;
    int res = min + (int) (Math.random() * ((max - min) + 1));

    par contre ça garantit pas que le même indice ne sortira pas 5x d'affilé ...

  13. #13
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    int min = 0;
    int max = number.length;
    int res;
    for(int j=0; j < 5 ; j++)		        			        	
    System.out.println(res = min + (int) (Math.random() * ((max - min) + 1)));
    en écrivant comme ça,
    il me donne le numéros que je n'avais pas dans mon array
    example:
    The original order:
    0: 1
    1: 3
    2: 7
    3: 8
    4: 10
    5: 13
    6: 17
    7: 18
    8: 19
    9: 20
    10: 23
    11: 24
    12: 26
    13: 27
    14: 29
    15: 32
    16: 33
    17: 35
    18: 39
    19: 41
    20: 43
    21: 46
    22: 47
    23: 48
    24: 49
    25: 50
    The new order:
    3: 8
    18: 39
    15: 32
    19: 41
    6: 17
    25: 50
    10: 23
    8: 19
    5: 13
    9: 20
    4: 10
    14: 29
    20: 43
    2: 7
    22: 47
    0: 1
    16: 33
    17: 35
    1: 3
    23: 48
    21: 46
    11: 24
    12: 26
    24: 49
    13: 27
    7: 18
    12
    12
    20
    9
    13

  14. #14
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    même en corrigeant string en int , j'ai erreur suivant:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator * is undefined for the argument type(s) double, int[]

    at another.main(another.java:36)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    import java.util.*;
    public class another 
    {
    	    public static void main(String[] arguments) 
    	    {
    	    	int number[] = { 1,3,7,8,10,13,17,18,19,20,23,24,26,
    	    	    			27,29,32,33,35,39,41,43,46,47,48,49,50 };
     
    		        int [] order = new int[number.length];
     
    		        System.out.println("The original order:");
    		        for (int i = 0; i < number.length; i++){
    		            order[i] = i;
    		            System.out.println(i + ": " + number[i]);
    	        }
     
    	        System.out.println("The new order:");
    	        int orderLen = number.length;
    		        for (int i = 0; i < number.length; i++)
    		        {
    		            int index = (int) (Math.random() * orderLen);
    		            int pos = order[index];
    		            System.out.println(pos + ": " + number[pos]);
    		            order[index] = order[--orderLen];  
     
    		        }
    		        int min = 0;
    		        int max = number.length;
    		        int res;
     
     
    		        	 for(int j=0; j < 5 ; j++)
     
    		        	System.out.println("Number"+ (j+1) + (int)(Math.random()*number));  
     
    		    }
    		}

  15. #15
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 566
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 566
    Points : 21 632
    Points
    21 632
    Par défaut
    Cela nous montre tout simplement qu'il ne fallait pas chercher à multiplier l'un et l'autre. Mais c'est ce que ta syntaxe donnait l'impression d'essayer de faire, donc bon...

    Dans ta réponse à franck06, j'ai pas compris :

    Ta première boucle affiche le contenu d'un tableau "number," ok. Mais la seconde affiche le contenu d'un tableau "order."

    C'est pas le même tableau. Et tu t'étonnes de pas avoir le même contenu ?!

Discussions similaires

  1. Sortir 6 numéros parmi 18 numéros
    Par domxaline dans le forum Débuter avec Java
    Réponses: 5
    Dernier message: 16/08/2012, 10h52
  2. récuperation d'un numéro de téléphone
    Par herzleid dans le forum Delphi
    Réponses: 4
    Dernier message: 17/07/2007, 09h25
  3. Empecher le curseur de sortir du dialog
    Par Gadjo dans le forum MFC
    Réponses: 5
    Dernier message: 18/11/2002, 18h01
  4. Calcul des numéros de semaine d'un calendrier
    Par Invité dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 06/11/2002, 21h29
  5. Sortir un typedef d'une classe
    Par Theophil dans le forum C++Builder
    Réponses: 13
    Dernier message: 03/07/2002, 17h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo