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 :

afficher le nombre random


Sujet :

avec Java

  1. #1
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut afficher le nombre random
    Bonjour,

    j'ai pas pu afficher le nombre hassard il m'affiche toujours
    java.util.Random@8814e9

    cmment faire pour afficher le nombre ?
    voila mon code :




    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
     
     
     
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JCheckBox;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
     
    import javax.swing.JTextField;
     
     
     
     
    public class Fenetre extends JFrame{
     
    	 private JPanel ecran = new JPanel();
    	 private JLabel titre= new JLabel("Le nombre Mystere !");
    	 private JLabel text1= new JLabel("Veuillez entrez le Nombre");
    	 private JTextField espace1 = new JTextField("");
    	 private JButton bouton_ok= new JButton("OK");
    	 private int lvl = 0;
    	 private Random rand = new java.util.Random();
    	 private JLabel text2= new JLabel("");
     
    	 public Fenetre(){
     
     
     
     
    		  this.setTitle("Le Nombre Mystere...");
    	        this.setSize(400, 400);
    	        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	        this.setLocationRelativeTo(null);
     
    	        ecran.setBackground(Color.WHITE);
    	        ecran.setLayout(new BorderLayout());
     
     
    	        espace1.setPreferredSize(new Dimension(100,20));
     
    	        bouton_ok.addActionListener(new ok());
     
     
     
    	        //===========================================================
     
    	        JOptionPane bienvenue;
    	        bienvenue = new JOptionPane();
     
    	        bienvenue.showMessageDialog(null, "Bienvenue dans le jeu...", "Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
     
    	    	String[] lvl = {"1-100", "1-500", "1-1000"};
    			JOptionPane jop = new JOptionPane(), jop2 = new JOptionPane();
    			String nom = (String)jop.showInputDialog(null, "Veuillez Chisir le niveau !","Niveau",JOptionPane.QUESTION_MESSAGE,null,lvl,lvl[2]);
     
    			//===========================================================
     
     
     
                if(nom.equals("1-100"))
                		{
               	 int rand = new Random().nextInt(100);
               	JOptionPane.showMessageDialog(null,"Le chiffre est: " + rand);
                		}
                if(nom.equals("1-500"))
        		{
     
               	 int rand = new Random().nextInt(500);
     
     
        		}
                if(nom.equals("1-1000"))
        		{
     
     
        		}
     
                System.out.println(rand);
     
     
     
    			JPanel top = new JPanel();
     
    			top.add(titre);
    			top.add(text1);
    			top.add(espace1);
    			top.add(bouton_ok);
    			top.add(text2);
     
     
     
    			ecran.add(top, BorderLayout.CENTER);
    			  this.setContentPane(ecran);
    		        this.setVisible(true); 
    	 }
     
    	   class ok implements ActionListener{
     
     
               public void actionPerformed(ActionEvent e) {
     
            	   if(espace1.getText().equals(rand))
            	   {
            		   System.out.println("true");
     
            	   }
            	   else
            	   {
            		   System.out.println("False");
     
     
            	   }
     
     
               }
     
        }
    }


    Merci d'avance.

  2. #2
    Modérateur
    Avatar de Alkhan
    Homme Profil pro
    ingénieur full stack
    Inscrit en
    Octobre 2006
    Messages
    1 232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : ingénieur full stack

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 232
    Points : 2 061
    Points
    2 061
    Par défaut
    bonjour,

    C'est normal
    Tu as des problèmes pour structurer ton code :
    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
    	private Random rand = new java.util.Random(); // tu declare rand (variable d'instance)
    .......
    		if (nom.equals("1-100")) {
    			int rand = new Random().nextInt(100); // encore un rand mais ici c'est un int dont la porté est le if
    			JOptionPane.showMessageDialog(null, "Le chiffre est: " + rand);
    		}
    		if (nom.equals("1-500")) {
    
    			int rand = new Random().nextInt(500); // encore un rand mais ici c'est un int dont la porté est le if
    
    		}
    		if (nom.equals("1-1000")) {
    
    		}
    
    		System.out.println(rand);// ici tu fais un print du rand (type Random) que tu as déclaré comme variable d'instance
    Il n'y a pas de problème, il n'y a que des solutions.
    Cependant, comme le disaient les shadoks, s'il n'y a pas de solution, c'est qu'il n'y a pas de problème.
    Si toutefois le problème persiste, la seule solution restante est de changer le périphérique qui se trouve entre la chaise et l'écran

    Mes Articles : Mon premier article est sur le language D
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Salut,

    Cela te marque java.util.Random@8814e9 parce que tu affiche un objet de type Random.

    Tu utilise une variable entière rand, qui masque ton attribut Random rand.
    Tu coup tu les confonds dans ton programme. D'ailleurs je ne voit pas pourquoi tu fait un new Rabdom() quand tu tire un nombre puisque tu dispose justement d'un attribut !
    Le mieux est d'utiliser un nom de variable différent.
    Au passage les méthodes show... de JOptionPane sont static, et le tirage d'un nombre exclue le maximum, il faut donc ajouter 1 pour avoir un nombre aléatoire commençant à 1.

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JTextField;
     
    @SuppressWarnings({ "nls", "serial" })
    public class Fenetre extends JFrame {
     
    	public static void main(String[] args) {
    		Fenetre f = new Fenetre();
    		f.setVisible(true);
    	}
     
    	private JPanel ecran = new JPanel();
    	private JLabel titre = new JLabel("Le nombre Mystere !");
    	private JLabel text1 = new JLabel("Veuillez entrez le Nombre");
    	private JTextField espace1 = new JTextField("");
    	private JButton bouton_ok = new JButton("OK");
    	private Random rand = new java.util.Random();
    	private JLabel text2 = new JLabel("");
     
    	public Fenetre() {
     
    		this.setTitle("Le Nombre Mystere...");
    		this.setSize(400, 400);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
     
    		this.ecran.setBackground(Color.WHITE);
    		this.ecran.setLayout(new BorderLayout());
     
    		this.espace1.setPreferredSize(new Dimension(100, 20));
     
    		this.bouton_ok.addActionListener(new ok());
     
    		// ===========================================================
     
    		JOptionPane.showMessageDialog(null, "Bienvenue dans le jeu...",
    				"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
     
    		String[] lvl = { "1-100", "1-500", "1-1000" };
    		String nom = (String) JOptionPane.showInputDialog(null,
    				"Veuillez Chisir le niveau !", "Niveau",
    				JOptionPane.QUESTION_MESSAGE, null, lvl, lvl[2]);
     
    		// ===========================================================
    		int nombre;
    		if (nom.equals("1-100")) {
    			nombre = this.rand.nextInt(100) + 1;
    		} else if (nom.equals("1-500")) {
    			nombre = this.rand.nextInt(500) + 1;
    		} else {
    			nombre = this.rand.nextInt(1000) + 1;
    		}
     
    		System.out.println("Nombre aléatoire : " + nombre);
     
    		JPanel top = new JPanel();
     
    		top.add(this.titre);
    		top.add(this.text1);
    		top.add(this.espace1);
    		top.add(this.bouton_ok);
    		top.add(this.text2);
     
    		this.ecran.add(top, BorderLayout.CENTER);
    		this.setContentPane(this.ecran);
    		this.setVisible(true);
    	}
     
    	class ok implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
     
    			if (Fenetre.this.espace1.getText().equals(Fenetre.this.rand)) {
    				System.out.println("true");
     
    			} else {
    				System.out.println("False");
     
    			}
     
    		}
     
    	}
     
    }

  4. #4
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Oups, désolé Alkhan , tu m'as grillé le temps d'écrire le message

  5. #5
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    Merci beaucoup pour la reponse,
    mais il reste un probleme
    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
     
    class ok implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
     
    			if (espace1.getText().equals(nombre)) {
    				System.out.println("true");
     
    			} else {
    				System.out.println("False");
     
    			}
     
    		}
     
    	}
    elle m'indique toujours false !

  6. #6
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Et bien, c'est une erreur du même genre.
    Tu compare une String (espace1.getText()) avec un objet de type Random (qui est un objet permettant un tirage aléatoire, mais n'est pas un nombre aléatoire). Cela a autant de sens de vouloir comparer des voitures avec des éléphants. Et de plus l'utilisation de la méthode equals avec des types différent renvoie toujours false. Ce n'est pas l'objet Random dont tu doit te servir à ce stade mais le nombre qui a été tirer. De plus on compare seulement des types identique.
    Tu devrais donc disposer d'un attribut référençant le nombre qui a été tirer aléatoirement. Ensuite du doit parser le texte de espace1 en Integer, et là tu pourra comparer les deux, et même savoir lequel est supérieur ou inférieur à l'autre pour réaliser ce célèbre jeux que nous avons tous réaliser à nos début

  7. #7
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Si tu bloque, dit le, et je te donnerais une solution possible.

  8. #8
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    j'ai pas trouver la solution

  9. #9
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JTextField;
     
    @SuppressWarnings({ "nls", "serial" })
    public class Fenetre extends JFrame {
     
    	public static void main(String[] args) {
    		Fenetre f = new Fenetre();
    		f.setVisible(true);
    	}
     
    	private JPanel ecran = new JPanel();
    	private JLabel titre = new JLabel("Le nombre Mystere !");
    	private JLabel text1 = new JLabel("Veuillez entrez le Nombre");
    	private JTextField espace1 = new JTextField("");
    	private JButton bouton_ok = new JButton("OK");
    	private Random rand = new java.util.Random();
    	private JLabel text2 = new JLabel("");
    	private Integer nombre;
     
    	public Fenetre() {
     
    		this.setTitle("Le Nombre Mystere...");
    		this.setSize(400, 400);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
     
    		this.ecran.setBackground(Color.WHITE);
    		this.ecran.setLayout(new BorderLayout());
     
    		this.espace1.setPreferredSize(new Dimension(100, 20));
     
    		this.bouton_ok.addActionListener(new ok());
     
    		// ===========================================================
     
    		JOptionPane.showMessageDialog(null, "Bienvenue dans le jeu...",
    				"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
     
    		String[] lvl = { "1-100", "1-500", "1-1000" };
    		String nom = (String) JOptionPane.showInputDialog(null,
    				"Veuillez Chisir le niveau !", "Niveau",
    				JOptionPane.QUESTION_MESSAGE, null, lvl, lvl[2]);
     
    		// ===========================================================
    		if (nom.equals("1-100")) {
    			this.nombre = Integer.valueOf(this.rand.nextInt(100) + 1);
    		} else if (nom.equals("1-500")) {
    			this.nombre = Integer.valueOf(this.rand.nextInt(500) + 1);
    		} else {
    			this.nombre = Integer.valueOf(this.rand.nextInt(1000) + 1);
    		}
     
    		System.out.println("Nombre aléatoire : " + this.nombre);
     
    		JPanel top = new JPanel();
     
    		top.add(this.titre);
    		top.add(this.text1);
    		top.add(this.espace1);
    		top.add(this.bouton_ok);
    		top.add(this.text2);
     
    		this.ecran.add(top, BorderLayout.CENTER);
    		this.setContentPane(this.ecran);
    		this.setVisible(true);
    	}
     
    	class ok implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
    			try {
    				Integer proposition = Integer.valueOf(Integer.parseInt(Fenetre.this.espace1.getText()));
    				int comparaison = Fenetre.this.nombre.compareTo(proposition);
    				if (comparaison == 0) {
    					System.out.println("trouvé");
    	 			} else if (comparaison < 0) {
    					System.out.println("trop grand"); 
    				} else {
    					System.out.println("trop petit"); 
    				}
    			} catch(NumberFormatException ex) {
    				// le texte de espace1 n'est pas un nombre
     
    			}
    		}
     
    	}
     
    }

  10. #10
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 171
    Points : 163
    Points
    163
    Par défaut
    Personnellement, j'utilise
    double rand = Math.random();
    pour générer un nombre aléatoire.

  11. #11
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    MERCI beaucoup encore visiwi
    une derniere demande, est ce que tu peu m'expliquer c'est quoi integer et s'utilise dans quel cas ? pour faire quoi ? (si t'as du temps bien sure)

    Bonne journee a toi

  12. #12
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Integer est un wrapper du type primitif int, en fait un objet représentant un entier. Cela s'utilise chaque fois que tu as besoin d'un entier sous la forme d'un objet plutôt que d'un type primitif.
    A noter que pour chaque type primitif existe un wrapper objet
    --> [ame="http://en.wikipedia.org/wiki/Primitive_wrapper_class"]w[/ame].

  13. #13
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    j'ai une petite question j'ai pas vu l'interet de faire un nouveau sujet

    dans mon code



    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
     
    @SuppressWarnings({ "nls", "serial" })
    public class Fenetre extends JFrame {
     
     
     
     
    	public static void main(String[] args) {
    		Fenetre f = new Fenetre();
    		f.setVisible(true);
    	}
     
    	private JPanel ecran = new JPanel();
    	private JLabel titre = new JLabel("");
    	private JLabel text1 = new JLabel("Veuillez entrez le nombre");
    	private JTextField espace1 = new JTextField("");
    	private JButton bouton_ok = new JButton("OK");
    	private Random rand = new java.util.Random();
    	private JLabel text2 = new JLabel("");
    	private int nbcoups = 0;
     
     
     
     
     
    	private String espace11 = espace1.getText();
    	private Integer nombre;
     
     
    	public Fenetre() {
     
    		this.setTitle("Le Nombre Mystere...");
    		this.setSize(400, 70);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
     
    		this.ecran.setBackground(Color.WHITE);
    		this.ecran.setLayout(new BorderLayout());
     
    		this.espace1.setPreferredSize(new Dimension(100, 20));
     
    		this.bouton_ok.addActionListener(new ok());
     
    		// ===========================================================
     
    		JOptionPane.showMessageDialog(null, "Bienvenue dans le jeu...",
    				"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
     
    		String[] lvl = { "1-100", "1-500", "1-1000" };
    	    String nom = (String) JOptionPane.showInputDialog(null,
    				"Veuillez Chisir le niveau !", "Niveau",
    				JOptionPane.QUESTION_MESSAGE, null, lvl, lvl[2]);
     
     
    		// ===========================================================
    		if (nom.equals("1-100")) {
    			this.nombre = Integer.valueOf(this.rand.nextInt(100) + 1);
    		} else if (nom.equals("1-500")) {
    			this.nombre = Integer.valueOf(this.rand.nextInt(500) + 1);
    		} else {
    			this.nombre = Integer.valueOf(this.rand.nextInt(1000) + 1);
    		}
     
    		System.out.println("Nombre aléatoire : " + nombre);
     
    		JPanel top = new JPanel();
     
    		text1.setForeground(Color.red);
    		top.add(this.titre);
    		top.add(this.text1);
    		top.add(this.espace1);
    		top.add(this.bouton_ok);
    		top.add(this.text2);
     
    		this.ecran.add(top, BorderLayout.CENTER);
    		this.setContentPane(this.ecran);
    		this.setVisible(true);
    	}
     
    	class ok implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
     
    			try {
    				Integer proposition = Integer.valueOf(Integer.parseInt(Fenetre.this.espace1.getText()));
    				int comparaison = Fenetre.this.nombre.compareTo(proposition);
    				if (comparaison == 0) {
    					System.out.println("trouvé");
    					JOptionPane.showMessageDialog(null, "Bravo ! le nombre c'etait bien "+nombre+"\n vous l'avez trouver en << "+nbcoups+" Coups >>",
    							"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
    					sauvgarde();
    	 			} else if (nombre < proposition) {
    					System.out.println("trop grand"); 
    					JOptionPane.showMessageDialog(null, "le nombre mystere est MOIN que "+proposition,
    							"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
    					nbcoups++;
    				} else {
    					System.out.println("trop petit"); 
    					JOptionPane.showMessageDialog(null, "le nombre mystere est PLUS que "+proposition,
    							"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
    					nbcoups++;
    				}
    			} catch(NumberFormatException ex) {
     
     
    			}
     
    		}
     
    	}
     
    	public void sauvgarde() {
     
    		FileWriter writer = null;
    		String texte = "\t niveau :score :" +nbcoups;
    		try{
    		     writer = new FileWriter("sauvgarde.txt", true);
    		     writer.write(texte,0,texte.length());
    		     writer.close();
     
    		}catch(IOException ex){
    		    ex.printStackTrace();
    		}
     
    	}
     
    }
    j'aimerai que j'enregistrer le niveau aussi avec la functin sauvgarder mais quand je met String texte = "\t niveau : "+nom+"score :" +nbcoups;

    i m'affiche une erreur sous nom pourtant nbcups il m'affiche aucune erreur

  14. #14
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    Je ne vois nul part une variable ou attribut nom définis dans ton programme, c'est sans doute cela ton erreur.

  15. #15
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    oui mais le probeme c'est comment l'identifier
    quand je fait par exemple public String nom;
    nom va etre NULL !

  16. #16
    Membre expérimenté
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Points : 1 340
    Points
    1 340
    Par défaut
    évidement, c'est à toi de l'instancier et de savoir qu'elle sera le contenu de ta chaine !

  17. #17
    Membre du Club
    Inscrit en
    Février 2009
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 77
    Points : 44
    Points
    44
    Par défaut
    c'est bon j'ai pu faire le jeu
    http://www.4shared.com/file/14348083...e_mystere.html

    Merci pour votre aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant][Math] Afficher un nombre sans exposant
    Par tanguy dans le forum API standards et tierces
    Réponses: 5
    Dernier message: 24/09/2012, 13h58
  2. Réponses: 8
    Dernier message: 18/04/2011, 14h46
  3. [C#] Afficher un nombre conséquent de données.
    Par Joad dans le forum ASP.NET
    Réponses: 3
    Dernier message: 22/04/2005, 15h38
  4. Réponses: 4
    Dernier message: 29/05/2004, 14h29
  5. [JSP][TOMCAT] Afficher le nombre de sessions en cours
    Par kitov dans le forum Tomcat et TomEE
    Réponses: 2
    Dernier message: 24/05/2004, 13h48

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