问题描述
- 一个简单计算器java程序,不知道为什么除法总是算错
-
一个简单计算器java程序,不知道为什么除法总是算错(不是全概率是高概率应该说,麻烦大大帮忙看看,非常感激package com.calculator; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.border.TitledBorder; import javax.swing.JTextField; import java.awt.FlowLayout; import javax.swing.BoxLayout; import java.awt.GridLayout; import com.jgoodies.forms.layout.FormLayout; import com.jgoodies.forms.layout.ColumnSpec; import com.jgoodies.forms.factories.FormFactory; import com.jgoodies.forms.layout.RowSpec; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.Component; import java.awt.Point; public class Calculator_ extends JFrame { private JPanel contentPane; private JTextField tf; private JButton b7; private JButton b8; private JButton b9; private JButton b4; private JButton b5; private JButton b6; private JPanel panel_1; private JButton b1; private JButton b2; private JButton b3; private JButton b0; private JButton bpoint; private JButton badd; private JButton bcut; private JButton bmul; private JButton bdiv; private JButton bresult; private JButton bempty; StringBuilder sb=new StringBuilder(""); StringBuilder sb2=new StringBuilder(""); char c; double d; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculator_ frame = new Calculator_(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Calculator_() { setTitle("calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 384, 279); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setBounds(0, 0, 432, 55); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 0)); tf = new JTextField(); tf.setEditable(false); panel.add(tf); tf.setColumns(10); panel_1 = new JPanel(); panel_1.setBounds(436, 53, -434, 260); contentPane.add(panel_1); b7 = new JButton("7"); b7.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("7"); tf.setText(sb.toString()); } }); b7.setBounds(0, 53, 80, 46); contentPane.add(b7); b8 = new JButton("8"); b8.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("8"); tf.setText(sb.toString()); } }); b8.setBounds(78, 53, 80, 46); contentPane.add(b8); b9 = new JButton("9"); b9.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { sb.append("9"); tf.setText(sb.toString()); } }); b9.setBounds(158, 53, 80, 46); contentPane.add(b9); b4 = new JButton("4"); b4.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("4"); tf.setText(sb.toString()); } }); b4.setBounds(0, 97, 80, 46); contentPane.add(b4); b5 = new JButton("5"); b5.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("5"); tf.setText(sb.toString()); } }); b5.setBounds(78, 97, 80, 46); contentPane.add(b5); b6 = new JButton("6"); b6.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { sb.append("6"); tf.setText(sb.toString()); } }); b6.setBounds(158, 97, 80, 46); contentPane.add(b6); b1 = new JButton("1"); b1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("1"); tf.setText(sb.toString()); } }); b1.setBounds(0, 144, 80, 46); contentPane.add(b1); b2 = new JButton("2"); b2.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("2"); tf.setText(sb.toString()); } }); b2.setBounds(78, 144, 80, 46); contentPane.add(b2); b3 = new JButton("3"); b3.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("3"); tf.setText(sb.toString()); } }); b3.setBounds(158, 144, 80, 46); contentPane.add(b3); b0 = new JButton("0"); b0.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("0"); tf.setText(sb.toString()); } }); b0.setBounds(0, 189, 158, 46); contentPane.add(b0); bpoint = new JButton("."); bpoint.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb.append("."); tf.setText(sb.toString()); } }); bpoint.setBounds(158, 189, 80, 46); contentPane.add(bpoint); badd = new JButton("+"); badd.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb2=sb; sb=new StringBuilder(""); c='+'; tf.setText(""); } }); badd.setBounds(236, 97, 66, 46); contentPane.add(badd); bcut = new JButton("-"); bcut.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb2=sb; sb=new StringBuilder(""); c='-'; tf.setText(""); } }); bcut.setBounds(236, 144, 66, 46); contentPane.add(bcut); bmul = new JButton("*"); bmul.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb2=sb; sb=new StringBuilder(""); c='*'; tf.setText(""); } }); bmul.setBounds(301, 97, 66, 46); contentPane.add(bmul); bdiv = new JButton("/"); bdiv.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb2=sb; sb=new StringBuilder(""); c='/'; tf.setText(""); } }); bdiv.setBounds(301, 144, 66, 46); contentPane.add(bdiv); bempty = new JButton("empty"); bempty.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { sb=new StringBuilder(""); sb2=new StringBuilder(""); tf.setText(""); } }); bempty.setBounds(236, 53, 131, 46); contentPane.add(bempty); bresult = new JButton("="); bresult.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { try { switch (c) { case '+': d = Double.parseDouble(sb.toString()) + Double.parseDouble(sb2.toString()); break; case '-': d = Double.parseDouble(sb.toString()) - Double.parseDouble(sb2.toString()); break; case '*': d = Double.parseDouble(sb.toString()) * Double.parseDouble(sb2.toString()); break; case '/': d = Double.parseDouble(sb.toString().trim()) / Double.parseDouble(sb2.toString().trim()); break; } tf.setText("" + d); } catch (NumberFormatException e) { tf.setText("输入数据格式出错,请清空重新输入"); } } }); bresult.setBounds(236, 189, 131, 45); contentPane.add(bresult); } }
解决方案
try {
switch (c) {
case '+':
d = Double.parseDouble(sb.toString())
+ Double.parseDouble(sb2.toString());
break;
case '-':
d = Double.parseDouble(sb.toString())
- Double.parseDouble(sb2.toString());
break;
case '*':
d = Double.parseDouble(sb.toString())
* Double.parseDouble(sb2.toString());
break;
case '/':
d = Double.parseDouble(sb.toString().trim())
/ Double.parseDouble(sb2.toString().trim());
break;
}
tf.setText("" + d);
} catch (NumberFormatException e) {
tf.setText("输入数据格式出错,请清空重新输入");
}
减法和除法都反了
应该是sb2除以或者减去sb
case '-':
d = Double.parseDouble(sb2.toString());
- Double.parseDouble(sb.toString())
break;
case '/':
d = Double.parseDouble(sb2.toString().trim())
/ Double.parseDouble(sb.toString().trim());
break;
解决方案二:
用BigDecimal做四则运算试试。
http://bbs.csdn.net/topics/390152581
解决方案三:
建议你参考这个源码
http://download.csdn.net/detail/li461125839/3425313
时间: 2025-01-02 15:55:09