Problème Java svp urgentttttttttt

OUALI212 Messages postés 1 Statut Membre -  
 anonyme -
Bonjour,
je me bloque dans un probleme de repetition de methodes
en fait j arrive pas a faire repeter des methodes


j ai 1 equation 1+2 =3:
1-- je dois caché le 1 en mettant un ? a ca place ==> ?+2=3 et choisir la bonne reponse parmis des boutons a coté
2-- apres l execution de la partie haute il faut mettre a la place de 2 la meme chose mais on changeant les parametres ==> 3+?=3
mon programes fais la 1 partie et la 2 eme et n' arrive pas a repeter il se bloque
merci
///////////////////////////////////main
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class MainCalcul {


public static void main(String[] args) {
VueEqOperande vv=new VueEqOperande(new EqOperande());
}}

///////////////////////////////////////////////////classe EqOperande




import java.util.*;

import javax.swing.text.StyledEditorKit.BoldAction;


public class EqOperande extends Observable {

String[] equation= new String[5];
String[] operator= new String[4];//
String[] operande= new String[5];
//hide cacher ? + 1=2 ; 1 4 6
//on a 5 chiffres et 4 operatores
//et il faut cacher 1 et garder les 4 3+?=4
String[] eqHide = new String[5];
String res;
String saisie;
int score = 0;

//definition de la methode EqOperande
public EqOperande(){
//Get s'emploi pour lire un attribut de la classe, c'est une fonction.
//Set s'emploi pour "écrire"une valeur à n attribut de la classe, c'est une procédure.
this.setOperande();
this.setOperator();

this.equation=this.getEquation();
this.eqHide= this.getEqHide(this.equation,0);
this.res=this.equation[0];
this.saisie=null;
}

/* public int get0or2(){
int r=((int)(0 + Math.random()));
return 2*r;
}
*/

public void setOperator(){
operator[0]="+";
operator[1]="-";
operator[2]="*";
operator[3]="/";
}

/**
* Genere aleatoirement trois operands pour l'equation
*/
public void setOperande(){

int min1 = 1; int max1 = 3;
operande[0]=Integer.toString((int) (min1 + Math.random() * (max1 - min1)));
int min2 = 4; int max2 = 5 ;
operande[1]=Integer.toString((int) (min2 + Math.random() * (max2 - min2)));
int min3 = 6; int max3 = 7 ;
operande[2]=Integer.toString((int) (min3 + Math.random() * (max3 - min3)));
int min4 = 8; int max4 =9 ;
operande[3]=Integer.toString((int) (min1 + Math.random() * (max1 - min1)));
}

/**
* Genere une equation
*/

public String[] getEquation(){
int mn=0;
int mx=3;
//on fait random pour l'operateur + - * / qui sont dans operator[]
int op=((int)(mn + Math.random() * (mx - mn)));
this.setOperande();
this.setOperator();
//la méthode parseInt essaye de convertir la chaine
//de caractères passée en paramètre en un entier.
Integer op1=Integer.parseInt(this.operande[0]);
Integer op2=Integer.parseInt(this.operande[1]);
//Integer op3=Integer.parseInt(this.operande[2]);

equation[0]=this.operande[0];
equation[1]=this.operator[op];
equation[2]=this.operande[1];
equation[3]="=";
if(this.operator[op].equals("/"))
equation[4]=Integer.toString(op1/op2);
else
if(this.operator[op].equals("*"))
equation[4]=Integer.toString(op1*op2);
else
if(this.operator[op].equals("+"))
equation[4]=Integer.toString(op1+op2);
else

equation[4]=Integer.toString(op1-op2);

return this.equation;
}

/**
* L'Equation inconnue à afficher
*/
public String[] getEqHide(String[] res,int j){
String[] r = new String[5];
for(int i=0;i<5;i++) r[i]=res[i];
r[j]="?";

return r;
}

public void notifyObs(){
//indiquer que cet objet a ete changer
this.setChanged();
//Previent tous les observateurs qu'un changement a eu lieu. (on
//ne peut pas utiliser comme nom notify car cette methode existe deja
//dans object).
//notifier les vieux observateur utiliséslorskon a un changement comme score
this.notifyObservers();
}
/**
* Score
*/

public void incrementScore() {
if(this.saisie.equals(this.res)){
score++;
this.notifyObs();
}
else{this.notifyObs();}

}


}


////////////////////////////////////////////classe VueEqOperande ///////////////////////////

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.CloseAction;




public class VueEqOperande extends JFrame implements Observer {
//private static String = null;
//creer un conteneur
Container pane = this.getContentPane();
EqOperande equ;
//Un JLabel sert à afficher du texte sur une interface.
JLabel[] label= new JLabel[5];
//creer des boutons
JButton b1,b2,b3,b4;
//taille des boutons
Dimension tailleBouton = new Dimension(90,60);
//creer des panel p et label s
JPanel p = new JPanel();
JLabel s = new JLabel();


public VueEqOperande(EqOperande e)
{
this.equ=e;
//titre
this.setTitle(" SuperBrain - Jeu de Calcul - EqOperande ");
//methodes de placement par defaut
FlowLayout fl = new FlowLayout(FlowLayout.CENTER);

pane.setLayout(fl);
this.setLabels();
//ajouter les boutons
this.setButtons();
//ajoputer le score
this.setScore();



//addObserver(Observer o) : permet à un observateur de
//« s'abonner » à l'observable, c'est à dire de demander
//à celui-ci de le prévenir quand il a été modifié.

// affiche la fenetre






this.pack();
//rendre la fenetre visible
this.setVisible(true);equ.addObserver(this);
}



public void setLabels()
{//5 parceke 3+1=4 zone texte
for(int j=0;j<5;j++)
{
label[j]= new JLabel(this.equ.eqHide[j]);
pane.add(label[j],BorderLayout.CENTER);
repaint();
}

}

public void setButtons()
{

b1 = new JButton(this.equ.operande[2]);
b1.setPreferredSize(tailleBouton);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( arg0.getSource() instanceof JButton)
equ.saisie = ((JButton)arg0.getSource()).getText();
equ.incrementScore();
equ.setOperande();
label[0].setText(b1.getText());
}
});
pane.add(b1);

b2 = new JButton(this.equ.operande[3]);
b2.setPreferredSize(tailleBouton);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( arg0.getSource() instanceof JButton)
equ.saisie = ((JButton)arg0.getSource()).getText();
equ.incrementScore();
equ.setOperande();
label[0].setText(b2.getText());

}
});
pane.add(b2);

b3 = new JButton(this.equ.operande[0]);
b3.setPreferredSize(tailleBouton);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( arg0.getSource() instanceof JButton)
equ.saisie = ((JButton)arg0.getSource()).getText();
//System.out.println(equ.saisie);
equ.incrementScore();
equ.setOperande();
label[0].setText(b3.getText());
// equ.incrementScore();
//System.out.println(equ.score);

}
});
pane.add(b3);
b4 = new JButton(this.equ.operande[1]);
b4.setPreferredSize(tailleBouton);
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( arg0.getSource() instanceof JButton)
equ.saisie = ((JButton)arg0.getSource()).getText();
//System.out.println(equ.saisie);
equ.incrementScore();
equ.setOperande();
label[0].setText(b4.getText());
// equ.incrementScore();
//System.out.println(equ.score);

}
});
pane.add(b4);

}

public void setScore(){
this.p.setBorder(BorderFactory.createTitledBorder(" Votre Score est :"));
this.p.setPreferredSize(new Dimension(130,100));
this.s.setText(" Score :"+this.equ.score);

p.add(s);
this.add(p);

}





@Override
public void update(Observable arg0, Object arg1) {


this.s.setText(Integer.toString(equ.score));
//ref3();
ref();


}

private void ref() {
pane.removeAll();


equ.getEquation();

this.equ.eqHide= this.equ.getEqHide(this.equ.equation,0);
this.equ.res=this.equ.equation[0];

this.setLabels();
this.setButtons();
this.setScore();
//ref3();

ref2();


}



private void ref2() {
// TODO Auto-generated method stub
pane.removeAll();


equ.getEquation();

this.equ.eqHide= this.equ.getEqHide(this.equ.equation,2);
this.equ.res=this.equ.equation[2];

this.setLabels();
this.setButtons();
this.setScore();
update(equ, pane);



}



private void ref3() {
// TODO Auto-generated method stub
if( this.equ.score ==4)
{removeAll();
JOptionPane.showMessageDialog(this,"Bonne Réponse votre Score final est :"+this.equ.score, "Resultat",
JOptionPane.INFORMATION_MESSAGE);

JOptionPane.showMessageDialog(this,"vous avez eu: "+this.equ.score+ "point"+" passer a la 2 eme partie ", "2jeu",
JOptionPane.INFORMATION_MESSAGE);
dispose();

//VueEqOperator v=new VueEqOperator(new EqOperator());
}

}


}
///////////////////
A voir également:

1 réponse

anonyme
 
Bonjour,
As-tu le dernière version de Java ? Si non, javara solutionnera ton problème. Voici le lien et il y a un tuto pour l'utilisation.
Bonne chance.
http://www.vista-xp.fr/forum/topic2490.html
0