显示java中除法问题的答案

时间:2014-04-19 00:06:08

标签: java swing

我有一个程序显示随机数学问题然后答案。

任何时候问题都是分裂,答案就是0。

我认为它是因为我需要使用float而不是int来显示答案。有人可以让我热门改变我的程序,以正确显示分区问题的答案。

这是我的计划。

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 the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(560,400); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(b1.getText().equals("Click For Answer"))
                {
                    jl.setText(problems.toString());
                    b = "Next Question";
                    b1.setText(b);
                }
                else
                {
                    problems.run();
                    jl.setText(problems.getQuestion());
                    b = "Click For 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 a = 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 = a + b;

         askQuestion(a + " + " + b + " = ");
     }

     private void subtract(final int a, final int b) {
         expected = a - b;

         askQuestion(a + " - " + b + " = ");
     }

     private void multiply(final int a, final int b) {
         expected = a * b;

         askQuestion(a + " * " + b + " = ");
     }

     private void divide(final int a, final int b) {
         expected = a / 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);
     }
}

2 个答案:

答案 0 :(得分:2)

您正在进行整数计算。这将导致int。无论您存储结果的数据类型都无关紧要。

例如

double d = 10/3; 

结果将是3.0而不是3.3333333

你可以通过任何一个浮动或双重来解决它

double d = (float)10/3; 

答案 1 :(得分:0)

原因是你正在潜水两个整数。将分子或分母乘以1.0,问题应该是固定的。在这种情况下,您还必须将预期的类型更改为float或double。