模数结果总是错误的

时间:2014-04-12 14:14:39

标签: java

我想创建一个简单的计算器。例如。 $ 21.37可以换成21美元硬币,0 50美分硬币,1 20美分硬币。但是,我的剩余结果总是错误的,我无法理解为什么。以下是我的代码,请帮助,非常感谢提前。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Question extends JFrame implements ActionListener
{
    private JLabel amt,dollar,fifty,title,result,error,creator;
    private TextField tb1,tb2,tb3;
    private JButton count;
    public Question()
    {
        setSize(400,300);
        JPanel contentPane = new JPanel(null);
        contentPane.setBorder(BorderFactory.createLineBorder(Color.gray));
        setContentPane(contentPane);
        count = new JButton("=");
        title = new JLabel("Calculate Change");
        amt = new JLabel("Enter the amount:");
        dollar = new JLabel("1 Dollar Coin:");
        fifty = new JLabel("50 Cent Coin:");
        error = new JLabel("wrong information.");
        creator = new JLabel("Created by ?????");
        Font bold = new Font("Arial",Font.BOLD,15);
        Font wrong = new Font("Arial",Font.ITALIC,12);
        Font smaller = new Font("Arial",Font.ITALIC,10);
        error.setForeground(Color.red);
        tb1 = new TextField(13);
        tb2=new TextField(12);
        tb3= new TextField(12);
        tb2.setEnabled(false);
        tb3.setEnabled(false);
        creator.setFont(smaller);
        error.setFont(wrong);
        title.setFont(bold);
        title.setBounds(130,-14,150,40);
        amt.setBounds(20, 20, 120, 20);
        dollar.setBounds(40, 45, 120, 20);
        fifty.setBounds(40, 70, 120, 20);
        tb1.setBounds(160, 21, 90, 24);
        tb2.setBounds(160, 45, 90, 20);
        tb3.setBounds(160, 70, 90, 20);
        error.setBounds(260,170,150,20);
        count.setBounds(100,200,90,20);
        creator.setBounds(240,220,180,20);
        contentPane.add(title);
        contentPane.add(amt);
        contentPane.add(dollar);
        contentPane.add(fifty);
        contentPane.add(tb1);
        contentPane.add(tb2);
        contentPane.add(tb3);
        contentPane.add(error);
        contentPane.add(creator);
        contentPane.add(count);
        count.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            //String input = tb1.getText();
            double convert = Double.parseDouble(tb1.getText());
            if((e.getSource() == count) && (convert >=1)) 
            {
                double result;
                result = convert /1;
                convert = convert % 1;
               int result2 = (int)result;
                String result3 = String.valueOf(result2);
                tb2.setText(result3);

            }
            if((convert >= 0.5) && (convert < 0.99))
            {
                tb3.setText("1");
                convert = convert % 0.5;
                  String result4 = String.valueOf(convert);
                error.setText(result4);
            }
           }catch(Exception ex)
        {
            error.setText("Invalid input.");
        }
    }

    public static void main(String []args)
    {
        Question frame = new Question();
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:1)

您应该格式化结果。所以而不是:

String result3 = String.valueOf(result2);

考虑一下:

String result3 = String.format("%.2f", result2);

这将删除尾随数字并围绕您的数字进行显示。

有关如何使用String.format(...)的详细信息,请查看java.util.Formatter API

答案 1 :(得分:1)

您使用% 0.5

会得到错误的结果
double convert = Double.parseDouble("21.37");

int oneDollarCoins = (int) convert; // throws away the fractions
System.out.println(oneDollarCoins); // prints 21
convert -= oneDollarCoins;
int cents = (int) (convert * 100);
System.out.println(cents); // prints 37
// modulo the cents % 50, 20, ...
// don't use % 0.5

对于小数字double应该没问题,但对于更大的数字,你可能会得到其他人提到的错误结果。