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

Langage Java Discussion :

[float] 2 chiffres après la virgule


Sujet :

Langage Java

  1. #21
    Modérateur

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

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 608
    Points
    21 608
    Par défaut
    Pourquoi i et i2 vaudraient-ils autre chose que zéro ?

    Question subsidiaire : bien entendu, l'addition, la soustraction et la multiplication donnent les bons résultats ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  2. #22
    Membre actif Avatar de pendoRa
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Mai 2007
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2007
    Messages : 317
    Points : 278
    Points
    278
    Par défaut
    Bonjour,
    Citation Envoyé par thelvin Voir le message

    Question subsidiaire : bien entendu, l'addition, la soustraction et la multiplication donnent les bons résultats ?
    Oui

    Je n'ai pas saisi, "pourquoi i, et i2 vaudraient ils autre chose de zero ??"




    Voici le code complet :

    * pour rappel, la division ne s'opére pas ( ex : 3/2 = NaN ).
    Merci

    CLASSE CALCULATRICE

    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
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
    public class Calculatrice extends JFrame {
     
     
    	private 	JTextField field1;
    	private 	int i, i2, action;
    	private 	float j1, j2;
       	String 		a, b, c;
     
     
     
    	private static final long serialVersionUID = 1L;
     
    	public Calculatrice() {
    		super();
    		bienvenue();
    	}
     
     
    	@SuppressWarnings("static-access")
     
    	public void bienvenue() {
    		JOptionPane pane = new JOptionPane();
     
    		ImageIcon img = new ImageIcon("images/calcu.png");
    		pane.showMessageDialog(null, "Bienvenue sur votre Calculatrice Java !", "Bonjour !", JOptionPane.OK_OPTION,  img);
    			if(JOptionPane.YES_NO_CANCEL_OPTION==1) {
    				System.out.println(JOptionPane.YES_NO_CANCEL_OPTION);
    				build();
    			}else {
    				// Quitter l'application
    				System.exit(0);
     
    			}	
    	}
     
    	public void build() {
     
    		setName("Calculatrice JAVA");
    		setTitle("(-_-)");// titre.
    		setVisible(true);
    		setSize(300, 210); // taille
    		setLocationRelativeTo(null); // centrage
    		setResizable(false); // non redimmensionnable
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // croix a droite pour fermer
    		setContentPane(buildContentPane()); // ajout a la fenetre de la methode buildcontenpane ( texte)
     
    	}
     
     
     
    		public JPanel buildContentPane() {
     
     
    		// JPanel...
    		final JPanel panel = new JPanel();
    		panel.setLayout(new GridBagLayout());
    		GridBagConstraints gbl = new GridBagConstraints(); 
    		gbl.insets = new Insets(1, 1, 1, 1);		
     
    		final JLabel labelTitre  = new JLabel("CALCULATRICE JAVA");
     
     
    				gbl.gridx = 0;
    				gbl.gridy = 0;
    				gbl.gridwidth = 2;
    				gbl.gridheight = 1;
    	    	panel.add(labelTitre, gbl);
     
     
     
     
     
    	    /////////////////////////////////////////////
    	    // 			CREATION DES BOUTONS		   //
    	    /////////////////////////////////////////////
     
     
            final JPopupMenu	popupMenu	= new JPopupMenu("Menu");
            final JMenuItem     itemRaz   	= new JMenuItem("RAZ");
            final JMenuItem     itemExit    = new JMenuItem("Quitter");
            final  JInternalFrame graphique = new JInternalFrame();
     
    	    JButton boutonResult    = new JButton("=");
    	    JButton boutonPlus 	    = new JButton("+");
    	    JButton boutonMoins     = new JButton("-");
    	    JButton boutonDiv       = new JButton("/");
    	    JButton boutonMulti     = new JButton("*");
    	    JButton boutonC         = new JButton("C");
    	    JButton boutonDelta     = new JButton("Delta");
    	    JButton boutonInfo      = new JButton("Aide");
    	    JButton boutonGraph		= new JButton("Graph.");
    	    ///////////////////////////////////////////
    	    JButton bouton1         = new JButton("1");
    	    JButton bouton2 		= new JButton("2");
    	    JButton bouton3		    = new JButton("3");
    	    JButton bouton4 		= new JButton("4");
    	    JButton bouton5 		= new JButton("5");
    	    JButton bouton6 		= new JButton("6");
    	    JButton bouton7 		= new JButton("7");
    	    JButton bouton8 		= new JButton("8");
    	    JButton bouton9 		= new JButton("9");
    	    JButton bouton0 		= new JButton("0");
     
    	    /////////////////////////////////
     
     
     
     
     
    	    // # 1
    	     gbl.gridx = 0;
    	     gbl.gridy = 3;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     gbl.fill = GridBagConstraints.HORIZONTAL;
    	    	panel.add(bouton1, gbl); 
    	    	bouton1.setBackground(Color.black);
    	     	bouton1.setForeground(Color.white);
     
    	    	// # 2
    	     gbl.gridx = 1;
    	     gbl.gridy = 3;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	    	panel.add(bouton2, gbl); 
    	    	bouton2.setBackground(Color.black);
    	     	bouton2.setForeground(Color.white);
     
    	    	// # 3
    	     gbl.gridx = 2;
    	     gbl.gridy = 3;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	    	panel.add(bouton3, gbl);
    	    	bouton3.setBackground(Color.black);
    	     	bouton3.setForeground(Color.white);
     
    	    	// # 4
    	     gbl.gridx = 3;
    	     gbl.gridy = 3;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(bouton4, gbl); 
    	     	bouton4.setBackground(Color.black);
    	     	bouton4.setForeground(Color.white);
     
    	     // #5
    	     gbl.gridx = 0;
    	     gbl.gridy = 4;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(bouton5, gbl); 
    	     	bouton5.setBackground(Color.black);
    	     	bouton5.setForeground(Color.white);
     
    	     // # 6
    	     gbl.gridx = 1;
    	     gbl.gridy = 4;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(bouton6, gbl);
    	     	bouton6.setBackground(Color.black);
    	     	bouton6.setForeground(Color.white);
     
    	     // # 7
    	     gbl.gridx = 2;
    	     gbl.gridy = 4;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(bouton7, gbl);
    	     	bouton7.setBackground(Color.black);
    	     	bouton7.setForeground(Color.white);
     
    	     // # 8
    	     gbl.gridx = 3;
    	     gbl.gridy = 4;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;  
    	     	panel.add(bouton8, gbl);
    	     	bouton8.setBackground(Color.black);
    	     	bouton8.setForeground(Color.white);
     
    	     // # 9
    	     gbl.gridx = 0;
    	     gbl.gridy = 5;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	        panel.add(bouton9, gbl);
    	        bouton9.setBackground(Color.black);
    	     	bouton9.setForeground(Color.white);
     
    	     // # 0 
    	     gbl.gridx = 1;
    	     gbl.gridy = 5;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(bouton0, gbl);  
    	     	bouton0.setBackground(Color.black); 
    	     	bouton0.setForeground(Color.white);
     
     
    	     // # Annuler ( RAZ )
    	     gbl.gridx = 2;
    	     gbl.gridy = 5;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1; 
    	     	panel.add(boutonC, gbl);
    	     	boutonC.setBackground(Color.white);
    	     	boutonC.setForeground(Color.black);
     
     
     
    	     // # Resultat
    	     gbl.gridx = 0;
    		 gbl.gridy = 6;
    		 gbl.gridwidth = 2;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonResult, gbl); 	     	
    	     	boutonResult.setBackground(Color.red);
     
     
    	     gbl.gridx = 2;
    	     gbl.gridy = 6;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonGraph, gbl);
    	     	boutonGraph.setBackground(Color.yellow);
     
     
    	     // # DELTA
    	     gbl.gridx = 3;
    	     gbl.gridy = 5;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonDelta, gbl);
    	     	boutonDelta.setBackground(Color.green);
     
     
     
    	     // # Informations
    		 gbl.gridx = 3;
    		 gbl.gridy = 6;
    		 gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonInfo, gbl); 
    	     	boutonInfo.setBackground(Color.YELLOW);
     
     
     
     
     
     
    	     // # Additionner
    	     gbl.gridx = 0;
    	     gbl.gridy = 2;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	      	panel.add(boutonPlus, gbl);
    	      	boutonPlus.setBackground(Color.white);
    	     	boutonPlus.setForeground(Color.black);
     
     
    	     // # Soustraire
    	     gbl.gridx = 1;
    	     gbl.gridy = 2;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	      	panel.add(boutonMoins, gbl); 
    	      	boutonMoins.setBackground(Color.white);
    	     	boutonMoins.setForeground(Color.black);
     
     
     
    	     // # Diviser
    	     gbl.gridx = 2;
    	     gbl.gridy = 2;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonDiv, gbl); 
    	     	boutonDiv.setBackground(Color.white);
    	     	boutonDiv.setForeground(Color.black);
     
    	     // # Multiplier
    	     gbl.gridx = 3;
    	     gbl.gridy = 2;
    	     gbl.gridwidth = 1;
    	     gbl.gridheight = 1;
    	     	panel.add(boutonMulti, gbl); 
    	     	boutonMulti.setBackground(Color.white);
    	     	boutonMulti.setForeground(Color.black);
     
    	     // #	Afficher
     
     
    	     	field1 = new JTextField(20);
        	    field1.setColumns(20);
    		    field1.setEditable(false);
     
     
    		    gbl.gridx = 0;
    		    gbl.gridy = 1;
    		    gbl.gridwidth = 4;
    		    gbl.gridheight = 1;
    		    panel.add(field1, gbl);
     
     
    			i=0;
    			field1.setText("" +i);
     
     
    		    /////////////////////////////////////////////
    		    // 		     MENU CLIC DROIT         	   //
    		    /////////////////////////////////////////////
     
     
    			addMouseListener(new MouseAdapter() {
    				public void mousePressed(MouseEvent e) {
     
    					if (SwingUtilities.isRightMouseButton(e)) {
     
    						popupMenu.add(itemRaz);
    						popupMenu.add(itemExit);
     
    						popupMenu.show(e.getComponent(), e.getX(), e.getY());
     
    					}
     
    				}
    			});
     
    			itemRaz.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent arg0) {
     
    					field1.setText(""+0);
    				}
    			});
     
     
    			itemExit.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent arg0) {
     
    				ImageIcon img2 = new ImageIcon("images/quit.jpg");
     
    					//JOptionPane.showConfirmDialog(null, "Voulez vous vraiment quitter ?", "A bientot! ", JOptionPane.YES_OPTION, img2);
    				int choix = JOptionPane.showConfirmDialog(null, "Voulez vous vraiment quitter ?", "A bientôt !", JOptionPane.OK_CANCEL_OPTION, i2, img2);
     
    					if(choix == JOptionPane.OK_OPTION) {
    					System.exit(0);
    				}
    				}
    			});
     
     
     
     
    	    /////////////////////////////////////////////
    	    // 		     VALEURS DES BOUTONS     	   //
    	    /////////////////////////////////////////////
     
     
     
     
     
    	    bouton1.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    			i2=i;
    			i=1;
     
    				System.out.println(i);
    				field1.setText("" +i);
     
    			}
     
    	    });
     
    	    bouton2.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    			i2=i;	
    			i=2;
     
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
    	    bouton3.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				i2=i;
    				i=3;
     
    				System.out.println(i);
    				field1.setText("" +i);	
    			}
     
    	    });
    	    bouton4.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				i2=i;
    				i=4;	
     
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
     
    	    bouton5.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    			i2=i;
    			i=5;
     
     
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
     
    	    bouton6.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    			i2=i;
    			i=6;
     
    				field1.setText("");
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
     
    	    bouton7.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				i2=i;
    			i=7;
    				field1.setText("");
    				System.out.println(i);
    				field1.setText("" +i);
     
    			}
    	    });
     
    	    bouton8.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				i2=i;
    			i=8;
    				field1.setText("");
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
     
    	    bouton9.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				i2=i;
    			i=9;
    				field1.setText("");
    				System.out.println(i);
    		    field1.setText("" +i);
    			}
     
    	    });
     
    	    bouton0.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    			i2=i;
    			i=0;
     
    				//field1.setText("");
    				System.out.println(i);
    				field1.setText("" +i);
    			}
     
    	    });
     
    	    boutonDelta.addActionListener(new ActionListener () {
    	    	@Override 
    	    	public void actionPerformed(ActionEvent arg0) {
     
    	    		fenetre();
     
    	    	}   	
     
    	    });
     
    	    boutonInfo.addActionListener(new ActionListener() {
    	    	@Override
    	    	public void actionPerformed(ActionEvent arg0) { 
     
    	    		JOptionPane.showMessageDialog(null, "Pour quitter l'application : faire un clic droit dans la zone du Titre -> 'Quitter'\nFormules : \nDelta : B²-4AC \n autres formules à ajouter...", "Aide", JOptionPane.INFORMATION_MESSAGE);
    	    }
     
    		});
     
     
    	    // METHODE DU GRAPHIQUE
     
    	    boutonGraph.addActionListener(new ActionListener () {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
     
    				fenetreGraph();
    			}
     
     
     
     
    	    });
     
     
     
     
     
    	    /////////////////////////////////////////////////
    	    // OPERATIONS DES BOUTONS + BOUTON RESULTAT    //
    	    /////////////////////////////////////////////////
     
    	    boutonPlus.addActionListener(new ActionListener() {
    	    	@Override
    	    	public void actionPerformed(ActionEvent arg0) {
    	    		action = 1;
    	    	}
    	    });
     
    	    boutonMoins.addActionListener(new ActionListener() {
    	    	@Override
    	    	public void actionPerformed(ActionEvent arg0) {
    	    		action = 2;
    	    	}
    	    });
     
    	    boutonMulti.addActionListener(new ActionListener(){
    	    	@Override 
    	    	public void actionPerformed(ActionEvent arg0) {
    	    		action = 3;
    	    	}
    	    });
     
    	    boutonDiv.addActionListener(new ActionListener() {
    	    	@Override
    	    	public void actionPerformed(ActionEvent arg0) {
    	    		action = 4;
    	    	}
    	    });
     
    	    boutonResult.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
     
    			    float   resultat = 0;
     
    				switch (action)  {
    				case 1: 
    					resultat = i+i2;
     
    					break;
    				case 2:
    					resultat = i-i2;
     
    					break;
    				case 3: 
    					resultat = i*i2;
     
    					break;
    				case 4: 
    					 resultat = j1/j2;
     
     
    				default:
     
    					break;
    				}
     
    				// Affichage du resultat sur le label
     
    				field1.setText(""+resultat);
    				if(resultat==0) {
     
    					field1.setText(""+0);
    				}
     
    			}
     
    	    });
     
    	    // Bouton CLEAR ==> RAZ
    	    boutonC.addActionListener(new ActionListener() {
     
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
     
     
    				field1.setText(""+0);
     
    			}
    	    });
    		return panel;
     
                   }
     
    // METHODE DE LA FENETRE GRAPHIQUE
     
    		private void fenetreGraph() {
     
     
    			final JFrame frameGraph 	= new JFrame();
    			final JPanel panelGraph  	= new JPanel();
    				  panelGraph.setLayout( new FlowLayout(FlowLayout.CENTER));
     
     
     
     
    			frameGraph.add(panelGraph);
     
    			frameGraph.setSize(350, 210);
    			frameGraph.setResizable(false);
    			frameGraph.setLocationRelativeTo(null);
    			frameGraph.setTitle("Fenêtre Graphique");
    			frameGraph.setBackground(Color.white);
    			frameGraph.setVisible(true);
    		}
     
     
     
    // METHODE DE LA FENETRE DELTA CALCUL
    		private void fenetre()   {
     
    			JButton confirm 			= new JButton("Calculer");
    			JButton cancel 				= new JButton("Annuler");
    			final JTextField field2	    = new JTextField(5);
    			final JTextField field3 	= new JTextField(5);
    			final JTextField field4 	= new JTextField(5);
    			JLabel labelDelta    		= new JLabel(" x² + ");
    			JLabel labelDelta2 			= new JLabel(" x + ");
    			JLabel labelDelta3 			= new JLabel("constante");
     
     
     
     
    			final JFrame deltaFrame 	= new JFrame();
    			final JPanel panelDelta 	= new JPanel();
    				  panelDelta.setLayout( new FlowLayout(FlowLayout.CENTER));
     
     
     
     
    			deltaFrame.add(panelDelta);
     
     
    			panelDelta.add(field2);
    			panelDelta.add(labelDelta);
    			panelDelta.add(field3);
    			panelDelta.add(labelDelta2);
    			panelDelta.add(field4);
    			panelDelta.add(labelDelta3);
     
     
     
    			// BOUTONS
     
        	    panelDelta.add(confirm); 
        	    confirm.setBackground(Color.black);
    	     	confirm.setForeground(Color.white);
     
     
        	    panelDelta.add(cancel);
        	    cancel.setBackground(Color.black);
    	     	cancel.setForeground(Color.white);
     
     
    	     	// Validation des 3 valeurs de la fonction f(x)
        	    confirm.addActionListener( new ActionListener() {
        	    	@Override 
        	    	public void actionPerformed(ActionEvent arg0){
     
     
        	    		String stringA = field2.getText();
        	    		int a = Integer.parseInt(stringA);
     
        	    		String stringB = field3.getText();
        	    		int b = Integer.parseInt(stringB);
     
        	    		String stringC = field4.getText();
        	    		int c = Integer.parseInt(stringC);
     
     
        	    		int   	solution = 0;
        	    		double  X1 = 0;
        	    		double  X2 = 0;
        	    		double  X0 = 0;
     
     
        	    		// Application de la formule delta = b²-4ac
        	    			solution = ((b*b)-(4*(a*c)));
     
        	    			// fonction apres bouton OK enclenché
        	    			if(JOptionPane.OK_OPTION == 0) {
     
     
        	    				// SI DELTA SUP A ZERO
        	    				if(solution>0) {
     
        	    					X1 = -b-(Math.sqrt(solution)/(2*a)); // 1ere solution
        	    					X2 =  b-(Math.sqrt(solution)/(2*a)); // 2eme solution
        	    	JOptionPane.showMessageDialog(null, "Delta =  " +solution +"\nDelta < 0, l'équation admet deux solutions\n X1 = "+X1+ "\n et X2 = "+X2, "Calcul Delta", JOptionPane.INFORMATION_MESSAGE);
     
     
     
        	    				// SI DELTA = 0	
        	    				}if(solution==0) {
     
        	    				    X0 = -b/(2*a);
        	    	JOptionPane.showMessageDialog(null,"\nDelta = 0 l'équation admet une solution,\n X0 = "+X0, "Calcul Delta", JOptionPane.INFORMATION_MESSAGE);
     
     
     
        	    			    // SI DELTA INF A ZERO 
        	    				}if(solution<0) {
     
        	    	JOptionPane.showMessageDialog(null, "Delta =  " +solution +"\nDelta < 0, l'équation n'admet aucune solution dans R", "Calcul Delta", JOptionPane.WARNING_MESSAGE);
     
        	    				}
        	    		}
        	    	}
        	    });
     
        	    // fonction exit de la fenetre Delta
        	    cancel.addActionListener( new ActionListener() {
        	    	@Override 
        	    	public void actionPerformed(ActionEvent arg0){
     
        	    		deltaFrame.setVisible(false);
        	    	}
        	    });
     
        	     	// PROPRITETES FENETRE DELTA
     
        	    	deltaFrame.setSize(350, 90);
        	    	deltaFrame.setResizable(false);
            		deltaFrame.setLocationRelativeTo(null);
            		deltaFrame.setTitle("[ Calcul Delta ] Rappel : (  f(x)= ax² + bx + c  )");
            		deltaFrame.setBackground(Color.white);
            		deltaFrame.setVisible(true);	
    		}	        
    	}
    CLASSE MAIN

    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
    import javax.swing.SwingUtilities;
     
    public class CalculatriceMain {
     
    	public static void main(String[] args){
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				Calculatrice fenetre = new Calculatrice();
     
    				fenetre.setVisible(true);
     
     
    			}
    		});
    	}
     
    }

  3. #23
    Modérateur

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

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 608
    Points
    21 608
    Par défaut
    Citation Envoyé par pendoRa Voir le message
    Bonjour,
    Question subsidiaire : bien entendu, l'addition, la soustraction et la multiplication donnent les bons résultats ?
    Oui
    Oui, forcément, elles, elles utilisent les int i et i2, alors que la division utilise les float j et j2.
    La différence est là.

    Je n'ai pas saisi, "pourquoi i, et i2 vaudraient ils autre chose de zero ??"
    Bah, c'est simple. Dans le code précédent, j'ai bien vu que tes opérations portaient sur des float nommés i et i2. Mais je n'ai pas vu le moindre code qui modifie leurs valeurs. Par conséquent, leurs valeurs étaient toujours zéro, et float zéro divisé par zéro donne NaN.

    Avec le code que tu viens de poster, nous apprenons qu'en réalité, i et i2 sont des ints, et que addition, soustraction et multiplication portent là-dessus.
    Par contre, la division porte sur les floats j et j2.
    Et, de fait, ils ne sont jamais modifiés et valent toujours zéro, la division est donc toujours zéro divisé par zéro, ce qui vaut toujours NaN.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  4. #24
    Membre actif Avatar de pendoRa
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Mai 2007
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2007
    Messages : 317
    Points : 278
    Points
    278
    Par défaut
    D'accord merci pour ta réponse précise. Quel oubli incroyable ! N'importe quoi !
    J'ai donc initialisé pour chaque chiffre j et j2.
    Ce qui fonctionne beaucoup mieux !

    Merci.


    Cordialement

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Float et affichage de 3 chiffres après la virgule sans arrondi
    Par Lolitaaa dans le forum Général Java
    Réponses: 3
    Dernier message: 20/05/2011, 10h59
  2. Réponses: 4
    Dernier message: 11/08/2009, 17h23
  3. Tronquer un float (n chiffres apres la virgule)
    Par franck06 dans le forum Langage
    Réponses: 8
    Dernier message: 16/06/2009, 17h58
  4. Réponses: 2
    Dernier message: 24/06/2008, 16h19
  5. Réponses: 4
    Dernier message: 06/12/2007, 20h23

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