Valid XHTML    Valid CSS2    

Listing du fichier nbm.java

 

00001     import java.applet.*;
00002     import java.awt.*;
00003     import java.awt.event.*;
00004     
00005     public class nbm extends java.applet.Applet implements ActionListener {
00006     
00007      static int maxE = 15 ; // 10 essais en tout au maximum
00008      int lnbm ; // le nombre à trouver
00009      int nbe = 0 ; // nombre d'essais ;
00010     
00011      Label slt1 = new Label("Entrer un nombre entier entre 1 et 200 puis cliquer sur le bouton Ok pour valider ") ;
00012      TextField pds2 = new TextField(10) ;
00013      Button bva3 = new Button(" Ok ") ;
00014      Label slt4 = new Label("") ;
00015     
00016      Panel lig1 = new Panel() ;
00017      Panel lig2 = new Panel() ;
00018      Panel lig3 = new Panel() ;
00019     
00020      String lrep ; // ligne de dialogue pour la réponse
00021     
00022      public static int entierAuHasard( int vmin, int vmax) {
00023      return (int) (vmin + Math.random()*(vmax-vmin)) ;
00024      } // fin de entierAuHasard
00025     
00026      public void init() {
00027     
00028      lnbm = entierAuHasard(1,200) ;
00029     
00030      setLayout( new GridLayout(3,1)) ;
00031     
00032      add(lig1) ;
00033      lig1.add(slt1) ;
00034     
00035      add(lig2) ;
00036      lig2.add(pds2) ;
00037      lig2.add(bva3) ;
00038      bva3.addActionListener(this) ;
00039     
00040      add(lig3) ;
00041      lig3.add(slt4) ;
00042      slt4.setText(" vous n'avez pas encore appuyé." + copies(" ",45)) ;
00043     
00044      } ; // fin de init
00045     
00046      public void actionPerformed(ActionEvent e) {
00047     
00048      String sais ;
00049      int nsai ;
00050      int ok ;
00051     
00052      nbe++ ;
00053     
00054      if (nbe>maxE) { fin(0) ; } else {
00055     
00056      sais = pds2.getText() ;
00057      nsai = 0 ;
00058      ok = 0 ;
00059     
00060      if ( sais.length() == 0 ) {
00061      lrep = " essai " + nbe + "/" + maxE + " : vous n'avez rien écrit." ;
00062      } else {
00063      lrep = " essai " + nbe + "/" + maxE + " : vous avez écrit "+pds2.getText() ;
00064      ok = 1 ;
00065      try {
00066      nsai = Integer.parseInt( sais ) ;
00067      } catch (Exception er) {
00068      lrep = lrep + ", ce n'est pas un entier." ;
00069      } ; // fin de try
00070      if (ok==1) {
00071      lrep = lrep + ", cet entier est " ;
00072      if (nsai<lnbm) { lrep += "trop petit." ; } ;
00073      if (nsai>lnbm) { lrep += "trop grand." ; } ;
00074      if (nsai==lnbm) { lrep += "le bon." ;
00075      fin(1) ;
00076      } ; // fin de si bon nombre
00077      } ; // fin de si ok = 1
00078      } ; // fin de si sais.length() = 0
00079     
00080      slt4.setText(lrep) ;
00081      } ; // fin de si trop d'essais
00082     
00083      repaint() ;
00084     
00085      } ; // fin de actionPerformed
00086     
00087      public void fin (int resF ) {
00088      slt1.setText(" recharger la page pour recommencer.") ;
00089      lig2.remove(pds2) ;
00090      lig2.remove(bva3) ;
00091      if (resF==0) {
00092      lrep = " vous n'avez pas trouvé le nombre " + lnbm + " en " + maxE + " essais." ;
00093      } else {
00094      lrep = " vous avez trouvé le nombre " + lnbm + " en " + nbe + " essai[s]." ;
00095      } ; // fin de si
00096      slt4.setText(lrep) ;
00097      } ; // fin de fin !
00098     
00099      public static String copies(String motif, int repet) {
00100      String chen = "" ;
00101      for (int ifois =1 ; ifois <= repet ; ifois++) {
00102      chen = motif + chen ;
00103      } ; // fin de pour
00104      return chen ;
00105      } // fin de copies
00106     
00107     
00108      } ; // fin de classe

Pour ne pas voir les numéros de ligne, ajoutez &nl=non à la suite du nom du fichier.

 

 

retour gH    Retour à la page principale de   (gH)