swing - displaying answers to division problems in java -


i have program shows random math problems answers.

anytime problem division answer comes out 0.

i think because need use float instead of int show answer. can show me hot change program display division problem answers correctly.

here program.

import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.event.*;  import javax.swing.*;  public class driver extends mathproblems {      mathproblems problems = new mathproblems();      string s = "welcome students!";     string b = "start!";     private jframe f;     private jpanel p;      jframe frame = new jframe();      jbutton b1 = new jbutton(b);      jlabel jl = new jlabel(s);      int i;      public driver () {               gui();       }      public void gui() {          f = new jframe("flash card program");                p = new jpanel();            f.setlayout( new gridlayout( 2, 1 ) );         f.add(jl);         f.add(p);         p.setlayout( new gridlayout( 2, 1 ) );         p.add(b1);          jl.sethorizontalalignment(jlabel.center);          // pack frame better cross platform support         f.pack();         // make visible         f.setvisible(true);         f.setsize(560,400); // default size 0,0         f.setdefaultcloseoperation(jframe.exit_on_close);           b1.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e){                 if(b1.gettext().equals("click answer"))                 {                     jl.settext(problems.tostring());                     b = "next question";                     b1.settext(b);                 }                 else                 {                     problems.run();                     jl.settext(problems.getquestion());                     b = "click answer";                     b1.settext(b);                  }             }         });     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {            public void run() {                 new driver();            }         });     } // end main method  } // end class driver 

mathproblems

import java.util.random;  public class mathproblems {      private static final int max_number = 10;      private static final random random = new random();       private int expected = 0;      private string question = "";       public void run() {          final int = random.nextint(max_number);          final int b = random.nextint(max_number);           final int type = random.nextint(4);           switch (type) {              case 0:                   add(a, b);                  break;              case 1:                  subtract(a, b);                 break;              case 2:                 multiply(a, b);                 break;              case 3:                  divide(a, b);                  break;          }      }       private void add(final int a, final int b) {          expected = + b;           askquestion(a + " + " + b + " = ");      }       private void subtract(final int a, final int b) {          expected = - b;           askquestion(a + " - " + b + " = ");      }       private void multiply(final int a, final int b) {          expected = * b;           askquestion(a + " * " + b + " = ");      }       private void divide(final int a, final int b) {          expected = / b;           askquestion(a + " / " + b + " = ");      }       private  void askquestion(final string question) {          this.question = question;      }         public string getquestion() {          return question;      }       public int getanswer() {          return expected;    }       @override      public string tostring(){      return integer.tostring(expected);      } } 

you doing integer calculation. result in int. doesn't matter, in data type storing result.

for example

double d = 10/3;  

result 3.0 not 3.3333333

you can solve making 1 float or double

double d = (float)10/3;  

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -