Pbl calculatrice java

Bonjour,
j'essai de faire une calculatrice trés simple sous java pour utiliser le switch/case
Mais je ne comprend pas comment celui-ci fonctionne.
Dans mon programme je voudrais lire 2 réel (a, b) et une variable (opération)

si l'opération vaut "+", alors j'affiche a+b
si l'opération vaut "-", alors j'affiche a-b
si l'opération vaut "*", alors j'affiche a*b
si l'opération vaut "/", alors j'affiche a/b
si l'opération vaut "+", alors j'affiche "opération non valide"

visiblement je ne doit pas comprendre car un grd nombre d'érreurs sont présentes
Si quelque pourais m'aider

voici ce que j'ai fait:

public static void (String[] args)
{
calculatrice();
}

public static void calculatrice()
{
double a,b,operation;
a=dialogue.lireRéel("entrez a");
b=dialogue.lireRéel("entrez b");
operation=dialogue.lireRéel("entrez l'operation");

switch (operation)
{
case 1: System.out.println("+"); break;
case 2: System.out.println("-"); break;
case 3: System.out.println("*"); break;
case 4: System.out.println("/"); break;
default: System.out.println("Operation non valide")
}
do
{
calculatrice():
}
while //opération choisie +,-;*ou/

}

Merci d'avance
Configuration: Windows XP
Internet Explorer 7.0

4 réponses

  • Votes
  • Date
  • Date décroissante
  1. Bonjour,

    operation est un String, il faudrait un int : switch (int)

    Peut-être en utilisant showOptionDialog

    Exemple:

    Object[] options = { "+", "-", "*", "/"};
    {
    int reponse = javax.swing.JOptionPane.showOptionDialog(
    null, "<+> ou <Intro> pour additionner,\n"
    + "ou <-> pour soustraire,\n"
    + "ou <*> pour multiplier,\n"
    + "ou </> pour diviser.\n",
    "Cliquez",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.PLAIN_MESSAGE,
    null, options, options[0]);
    switch(reponse)
    {
    case 1: System.out.println("+"); break;
    case 2: System.out.println("-"); break;
    case 3: System.out.println("*"); break;
    case 4: System.out.println("/"); break;
    default: System.out.println("Operation non valide") ;
    }
    }

    Bon ce n'est pas très adapté à une calculatrice, mais tu vois une utilisation de switch/case et en plus celle de javax.swing.JOptionPane.showOptionDialog

    Cordialement,

    Dan
    1. PS,

      J'ai oublié de modifier les "case", reponse prenant les valeurs 0 à 3, il faut bien entendu :

      case 0: System.out.println("+"); break;
      case 1: System.out.println("-"); break;
      case 2: System.out.println("*"); break;
      case 3: System.out.println("/"); break;

      Dan
      1. Voila le code de calcul avec java

        import java.awt.*;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.*;

        public class Calcul extends JFrame implements ActionListener{
        JTextField affichage =new JTextField(20);
        JButton b1=new JButton("1");
        JButton b2=new JButton("2");
        JButton b3=new JButton("3");
        JButton b4=new JButton("4");
        JButton b5=new JButton("5");
        JButton b6=new JButton("6");
        JButton b7=new JButton("7");
        JButton b8=new JButton("8");
        JButton b9=new JButton("9");
        JButton bPlus=new JButton("+");
        JButton bMoins=new JButton("-");
        JButton bMult=new JButton("*");
        JButton bDiv=new JButton("/");
        JButton b0=new JButton("0");
        JButton bResult=new JButton("=");
        JButton bReset=new JButton("C");

        public Calcul(){
        JPanel p1=new JPanel();
        FlowLayout layout1 =new FlowLayout(FlowLayout.CENTER);

        JPanel p2=new JPanel();
        GridLayout layout2 =new GridLayout(3,3);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);

        JPanel p3=new JPanel();
        FlowLayout layout3 =new FlowLayout();
        bPlus.addActionListener(this);
        bMoins.addActionListener(this);
        bMult.addActionListener(this);
        bDiv.addActionListener(this);
        b0.addActionListener(this);
        bResult.addActionListener(this);
        bReset.addActionListener(this);

        setTitle("Calcul");
        setSize(350,250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        Container pane=getContentPane();
        GridLayout layout4 =new GridLayout(3,1);
        //pane.setLayout(layout4);
        pane.add(p1,BorderLayout.NORTH);
        pane.add(p2,BorderLayout.CENTER);
        pane.add(p3,BorderLayout.SOUTH);
        setContentPane(pane);

        p1.setLayout(layout1);
        p1.add(affichage);

        p2.setLayout(layout2);
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(b7);
        p2.add(b8);
        p2.add(b9);

        b1.setFont(new Font("Bernard MT Condensed",Font.BOLD,18));

        p3.setLayout(layout3);
        p3.add(bPlus);
        p3.add(bMoins);
        p3.add(bMult);
        p3.add(bDiv);
        p3.add(bResult);
        p3.add(b0);
        p3.add(bReset);

        }
        public String g(JButton b){
        return b.getText();
        }
        Double vcour=new Double(0);
        char operateur=' ' ;

        public void actionPerformed(ActionEvent ev) {
        Double vaffich=new Double(0);
        JButton bclic =(JButton)ev.getSource();
        // JButton bclic=null ;
        // bclic=(JButton) sourceEvénement;

        String ecr=affichage.getText();
        if(!"".equals(ecr)){
        vaffich=Double.parseDouble(ecr);
        }
        if(bclic==bPlus){
        vcour=vaffich;
        operateur='+';
        affichage.setText("");
        }else{
        if(bclic==bMoins){
        vcour=vaffich;
        operateur='-';
        affichage.setText("");
        }else{
        if(bclic==bMult){
        vcour=vaffich;
        operateur='*';
        affichage.setText("");
        }else{
        if(bclic==bDiv){
        vcour=vaffich.doubleValue();
        operateur='/';
        affichage.setText("");
        }else{
        if(bclic==bResult){
        if(operateur=='+'){
        vcour+=vaffich;

        affichage.setText(vcour.toString());
        }
        if(operateur=='-'){
        vcour-=vaffich;
        affichage.setText(vcour.toString());
        }
        if(operateur=='*'){
        vcour*=vaffich;
        affichage.setText(vcour.toString());
        }
        if(operateur=='/'){
        vcour/=vaffich;
        affichage.setText(vcour.toString());
        }
        }else{
        if(bclic==bReset){
        affichage.setText("");
        }else{
        affichage.setText(affichage.getText()+bclic.getText());
        }

        }
        }
        }
        }
        }

        }
        public static void main(String []args){
        Calcul cal=new Calcul();
        }

        }
        1. Pourquoi il y a 6 { avant void main
        2. @Hacker#1Salut,

          Où as tu vu 6 { avant main, moi je constate qu'il y a 7 } et c'est qu'il y a 7 { à fermer:

          Les 6 1ères } correspondent aux 6 else{
          la 7ème } correspond à public void actionPerformed(ActionEvent ev) {

          Dan
      2. Salut,

        C'est bien joli fatimaezz, mais la question est, comment fonctionne switch/case? et j'ai répondu avec un petit exemple.

        Cordialement,

        Dan