终于写完了一个简单的计算器,但是有一个除数为零的异常问题,请各位指教 你使用Float来计算很可能会出现问题,把一个0转成浮点数后很可能它在机器中的值并不完全等于0,而是无限接近于0的0.0000...0001,所以最后除法的结果并没有抛出异常,计算结果为无穷大。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以不用抛出异常来解决的,在做case时的除法,先判断一下n3是否为0 帮你重新设计了一下计算方法import java.awt.*;import java.awt.event.*;class jm implements ActionListener{ TextField tfd; Button t1,t2,t3,t4,t5,t6,t7,t8,t9,t0,t00,tjia,tjian,tcheng,tchu,tdian,jisuan; String n4="",n1,n2; String n3; public void jm1() { Frame f=new Frame("计算器界面"); Panel a=new Panel(); Panel b=new Panel(); tfd=new TextField("",15); b.add(tfd); jisuan=new Button("计算"); jisuan.addActionListener(this); b.add(jisuan); f.add(b,"North"); a.setLayout(new GridLayout(4,4)); t1=new Button("1"); t2=new Button("2"); t3=new Button("3"); t4=new Button("4"); t5=new Button("5"); t6=new Button("6"); t7=new Button("7"); t8=new Button("8"); t9=new Button("9"); t0=new Button("归零"); t00=new Button("0"); tjia=new Button("+"); tjian=new Button("-"); tcheng=new Button("*"); tchu=new Button("/"); tdian=new Button("."); a.add(t1); t1.addActionListener(this); a.add(t2); t2.addActionListener(this); a.add(t3); t3.addActionListener(this); a.add(tjia); tjia.addActionListener(this); a.add(t4); t4.addActionListener(this); a.add(t5); t5.addActionListener(this); a.add(t6); t6.addActionListener(this); a.add(tjian); tjian.addActionListener(this); a.add(t7); t7.addActionListener(this); a.add(t8); t8.addActionListener(this); a.add(t9); t9.addActionListener(this); a.add(tcheng); tcheng.addActionListener(this); a.add(t0); t0.addActionListener(this); a.add(t00); t00.addActionListener(this); a.add(tdian); tdian.addActionListener(this); a.add(tchu); tchu.addActionListener(this); f.add(a); f.setSize(200,200); f.setVisible(true); f.addWindowListener(new mywinlistener()); } public String jisuan(String n1,String n2,String n4) { int oo=0; if (n4.equals("+")) { oo=1; } else if (n4.equals("-")) { oo=2; } else if (n4.equals("*")) { oo=3; } else if (n4.equals("/")) { oo=4; } switch (oo) { case 1: n3=String.valueOf(Float.parseFloat(n1)+Float.parseFloat(n2)); break; case 2: n3=String.valueOf(Float.parseFloat(n1)-Float.parseFloat(n2)); break; case 3: n3=String.valueOf(Float.parseFloat(n1)*Float.parseFloat(n2)); break; case 4: if(Float.parseFloat(n2)==0) { n3="除数为零异常"; n4=""; n1=""; n2=""; break;} n3=String.valueOf(Float.parseFloat(n1)/Float.parseFloat(n2)); default: break; } return n3; } public void actionPerformed(ActionEvent e1) { if (e1.getActionCommand().equals("计算")) //如果按钮为计算,则计算结果 { tfd.setText(jisuan(n1,n2,n4)); } else if (e1.getActionCommand().equals("归零")) //如果按钮为归零,则清零。 { n4=""; n1=""; n2=""; n3="0"; tfd.setText(""); } else if ((e1.getActionCommand().equals("+")) || (e1.getActionCommand().equals("-")) || (e1.getActionCommand().equals("*")) || (e1.getActionCommand().equals("/"))) //如果为加减成除,则为符号。 { if (n4=="") { tfd.setText(""); n4=e1.getActionCommand(); } else if ((n2!="") && (n1!="")) { jisuan(n1,n2,n4); n1=String.valueOf(n3); n2=""; n4=e1.getActionCommand();; tfd.setText(""); } } else //如果什么都不是,则计数。 { if (n4!="") { //如果点了小数点 if (e1.getActionCommand().equals(".")) { String l; l=tfd.getText(); int s; s=l.indexOf("."); //判断是否有小数点。没有则加上。 if (s==-1) { n2= (tfd.getText()+e1.getActionCommand()); } } else { n2= (tfd.getText()+e1.getActionCommand()); } tfd.setText(n2); } else { //如果点了小数点 if (e1.getActionCommand().equals(".")) { String l; l=tfd.getText(); int s; s=l.indexOf("."); //判断是否有小数点。没有则加上。 if (s==-1) { n1=(tfd.getText()+e1.getActionCommand()); } } else { n1=String.valueOf(tfd.getText()+e1.getActionCommand()); } tfd.setText(n1); } } }}public class jiemian{ public static void main(String [] args) { jm i=new jm(); i.jm1(); }}//窗口响应事件class mywinlistener extends WindowAdapter{ public void windowClosing(WindowEvent e) { e.getWindow().setVisible(false); ((Window)e.getComponent()).dispose(); System.exit(0); }} 很奇怪,为什么"Float计算很可能会出现把一个0转成浮点数后,它在机器中的值并不完全等于0,而是无限接近于0的0.0000...0001,所以最后除法的结果并没有抛出异常,计算结果为无穷大。",这个实验的结果是无穷大,异常不响应。可是楼上的朋友修改后,float却又可以==0??这是为什么? 也许这样解释比较好,在float型和int型两种不同类型的数值之间进行比较,系统会把这两个数值自动转换成同一类型的,也就是int型的0会先转成float型的0(0.000...000f)再进行比较,所以==就成立了。 那最后请问一下zrtl(刚从软件园回来)如果不用Float的话,用什么类型的数据不会出现这种问题呢?要用异常的话,应该怎么做???最后的相关问题,我明白了就立刻结贴加分。 import java.awt.event.*;import java.awt.*;import javax.swing.event.*;import javax.swing.*;public class Test extends JFrame{ JButton[] number = new JButton[10]; JButton[] operator = new JButton[6]; String[] operatorValue = {"+","-","*","/","=","清零"}; JPanel deskTop; JPanel one,two; JTextArea msg; int first, //The FirstNumber second, //The secondNumber end, //The endNumber whichOperator; //"+"or"-"Or"*"or"/" public Test(){ try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public void jbInit() throws Exception{ msg = new JTextArea(5,5); one = new JPanel(); //Text'Panel two = new JPanel();//Button'Panel deskTop = (JPanel)getContentPane(); deskTop.setLayout(new BorderLayout()); deskTop.add(one,BorderLayout.NORTH); deskTop.add(two,BorderLayout.CENTER); one.setLayout(new BorderLayout()); one.setBackground(Color.red); two.setBackground(Color.blue); one.add(msg,BorderLayout.CENTER); two.setLayout(new FlowLayout()); //窗口关闭事件 this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); for(int i = 0;i<number.length;i++){ number[i] = new JButton(String.valueOf(i)); number[i].addActionListener(new number_Action()); two.add(number[i]); } for (int i = 0; i < operator.length; i++) { operator[i] = new JButton(operatorValue[i]); operator[i].addActionListener(new operator_Action()); operator[i].setActionCommand(String.valueOf(i)); two.add(operator[i]); } } private class number_Action implements ActionListener{ public void actionPerformed(ActionEvent ae){ for(int i= 0;i<10;i++){ if(ae.getSource().equals(number[i])){ msg.append(String.valueOf(i)); } } } } private class operator_Action implements ActionListener{ /**获取第一个参数的值 * */ public void getFirstValue(){ first = Integer.parseInt(msg.getText()); msg.setText(""); } public void getSecondValue(){ second = Integer.parseInt(msg.getText()); } public void actionPerformed(ActionEvent ae){ switch (Integer.parseInt(ae.getActionCommand())){ case 0: //+ whichOperator = 0; getFirstValue(); break; case 1://- whichOperator = 1; getFirstValue(); break; case 2://* whichOperator = 2; getFirstValue(); break; case 3://"/" whichOperator = 3; getFirstValue(); break; case 4://"=" switch (whichOperator){ case 0://+ getSecondValue(); end = first + second; System.out.println(end); break; /** * getSecondValue(); end = first + second; msg.setText(String.valueOf(end)); break;*/ case 1://- getSecondValue(); end = first - second; System.out.println(end); break; case 2://* getSecondValue(); end = first * second; System.out.println(end); break; case 3://"/" getSecondValue(); try { end = first / second; System.out.println(end); } catch (Exception ex) { JOptionPane.showMessageDialog(null,"除数不能零"); msg.setText(""); } JOptionPane.showMessageDialog(null,"/"); break; } case 5://"清零" msg.setText(""); break; } } } public static void main(String[] args){ Test sample = new Test(); sample.setSize(new Dimension(300,300)); sample.setVisible(true); }}兄弟帮帮们`看看咋滴了` 狼,看了一下,有很多问题哦!:)第一,不能连续进行计算。你这里的连续计算结果不对。我的这个可以!:)第二,计算结果不是显示在text里。是显示在DOS窗口里。我的这个显示在test里!:)第三,有和我的今天中午发现的一样的问题,加减剩除不能多按,否则出错!:)第四,你的计算器,只要等于了,在按任何符号,都会出错。我的是按了等号后,还可以继续运算。呵呵,大概了试用了一下,发现了以上的问题。你也试用一下我的,给我提提意见啊。我今天中午搞了半天,才发现,我的计算器,第一次运算的时候,按任何符号,都不会出错,而进行连算第二次的时候,多按一下符号就出错,郁闷死我了。不过你有比我做的好的地方,就是,你的异常处理的比我好。羡慕。呵呵,想起来了,偶还比你多一个小数点功能,哈哈~~~:)==============================================================================最后请问一下zrtl(刚从软件园回来)如果不用Float的话,用什么类型的数据不会出现这种问题呢?要用异常的话,应该怎么做???最后的相关问题,我明白了就立刻结贴加分。 用int、short或者long型的不会出现这种问题,但你的计算器需要有小数点功能那就需要用double或float型的。另外如果你一定需要用抛出异常来解决这个问题,可以考虑把被除数和除数都用BigDecimal进行构造,按需要取出intValue()或floatValue(),不过我并不推荐这样做,太变扭。或者你主动抛出一个new Exception试试。 哦,还是不太明白!:)还没有学熟悉异常!:)小水,点图标就执行可以啊!:)你装好JDK后,事先编译好,然后新建个快捷方式,方式的指向就是java运行命令就行!:)JAVA程序不进行编译成为CLASS是不能运行的!:)要点图标运行的话,只有我这个方法!:) 怎么用正则表达式,取出特定的字符? 重构新手问题 Calendar类的同一段代码运行两次得到的结果不同是怎么回事? 我如果确定每次传入的是不是同一类型 IO写文件的问题? 如何去掉除了换行和回车以外的空白字符 UltraEdit不能输入问题 得到系统目录!!! 一个关于ArrayList类的问题 在JAVA和.NET之间,你选哪一个? 绝对简单,就是我不会,大家帮忙啊 使用wait() 和 notify() 在两个线程类之间共享对象问题
import java.awt.*;
import java.awt.event.*;class jm implements ActionListener
{
TextField tfd;
Button t1,t2,t3,t4,t5,t6,t7,t8,t9,t0,t00,tjia,tjian,tcheng,tchu,tdian,jisuan;
String n4="",n1,n2;
String n3;
public void jm1()
{
Frame f=new Frame("计算器界面");
Panel a=new Panel();
Panel b=new Panel(); tfd=new TextField("",15);
b.add(tfd);
jisuan=new Button("计算");
jisuan.addActionListener(this);
b.add(jisuan);
f.add(b,"North");
a.setLayout(new GridLayout(4,4));
t1=new Button("1");
t2=new Button("2");
t3=new Button("3");
t4=new Button("4");
t5=new Button("5");
t6=new Button("6");
t7=new Button("7");
t8=new Button("8");
t9=new Button("9");
t0=new Button("归零");
t00=new Button("0");
tjia=new Button("+");
tjian=new Button("-");
tcheng=new Button("*");
tchu=new Button("/");
tdian=new Button(".");
a.add(t1);
t1.addActionListener(this);
a.add(t2);
t2.addActionListener(this);
a.add(t3);
t3.addActionListener(this);
a.add(tjia);
tjia.addActionListener(this);
a.add(t4);
t4.addActionListener(this);
a.add(t5);
t5.addActionListener(this);
a.add(t6);
t6.addActionListener(this);
a.add(tjian);
tjian.addActionListener(this);
a.add(t7);
t7.addActionListener(this);
a.add(t8);
t8.addActionListener(this);
a.add(t9);
t9.addActionListener(this);
a.add(tcheng);
tcheng.addActionListener(this);
a.add(t0);
t0.addActionListener(this);
a.add(t00);
t00.addActionListener(this);
a.add(tdian);
tdian.addActionListener(this);
a.add(tchu);
tchu.addActionListener(this);
f.add(a);
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new mywinlistener());
} public String jisuan(String n1,String n2,String n4)
{
int oo=0;
if (n4.equals("+"))
{
oo=1;
}
else if (n4.equals("-"))
{
oo=2;
}
else if (n4.equals("*"))
{
oo=3;
}
else if (n4.equals("/"))
{
oo=4;
} switch (oo)
{
case 1:
n3=String.valueOf(Float.parseFloat(n1)+Float.parseFloat(n2));
break;
case 2:
n3=String.valueOf(Float.parseFloat(n1)-Float.parseFloat(n2));
break;
case 3:
n3=String.valueOf(Float.parseFloat(n1)*Float.parseFloat(n2));
break;
case 4:
if(Float.parseFloat(n2)==0)
{
n3="除数为零异常";
n4="";
n1="";
n2="";
break;}
n3=String.valueOf(Float.parseFloat(n1)/Float.parseFloat(n2));
default:
break;
}
return n3;
}
public void actionPerformed(ActionEvent e1)
{ if (e1.getActionCommand().equals("计算"))
//如果按钮为计算,则计算结果
{
tfd.setText(jisuan(n1,n2,n4));
}
else if (e1.getActionCommand().equals("归零"))
//如果按钮为归零,则清零。
{
n4="";
n1="";
n2="";
n3="0";
tfd.setText("");
}
else if ((e1.getActionCommand().equals("+")) || (e1.getActionCommand().equals("-")) || (e1.getActionCommand().equals("*")) || (e1.getActionCommand().equals("/")))
//如果为加减成除,则为符号。
{ if (n4=="")
{
tfd.setText("");
n4=e1.getActionCommand();
}
else if ((n2!="") && (n1!=""))
{
jisuan(n1,n2,n4);
n1=String.valueOf(n3);
n2="";
n4=e1.getActionCommand();;
tfd.setText("");
}
}
else
//如果什么都不是,则计数。
{
if (n4!="")
{
//如果点了小数点
if (e1.getActionCommand().equals("."))
{
String l;
l=tfd.getText();
int s;
s=l.indexOf("."); //判断是否有小数点。没有则加上。
if (s==-1)
{
n2= (tfd.getText()+e1.getActionCommand());
}
}
else
{
n2= (tfd.getText()+e1.getActionCommand());
}
tfd.setText(n2);
}
else
{
//如果点了小数点
if (e1.getActionCommand().equals("."))
{
String l;
l=tfd.getText();
int s;
s=l.indexOf("."); //判断是否有小数点。没有则加上。
if (s==-1)
{
n1=(tfd.getText()+e1.getActionCommand());
}
}
else
{
n1=String.valueOf(tfd.getText()+e1.getActionCommand());
}
tfd.setText(n1);
}
}
}
}public class jiemian
{
public static void main(String [] args)
{
jm i=new jm();
i.jm1();
}
}//窗口响应事件
class mywinlistener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
}
如果不用Float的话,用什么类型的数据不会出现这种问题呢?
要用异常的话,应该怎么做???最后的相关问题,我明白了就立刻结贴加分。
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;public class Test
extends JFrame{
JButton[] number = new JButton[10];
JButton[] operator = new JButton[6];
String[] operatorValue = {"+","-","*","/","=","清零"};
JPanel deskTop;
JPanel one,two;
JTextArea msg;
int first, //The FirstNumber
second, //The secondNumber
end, //The endNumber
whichOperator; //"+"or"-"Or"*"or"/"
public Test(){
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
} public void jbInit() throws Exception{
msg = new JTextArea(5,5);
one = new JPanel(); //Text'Panel
two = new JPanel();//Button'Panel
deskTop = (JPanel)getContentPane();
deskTop.setLayout(new BorderLayout());
deskTop.add(one,BorderLayout.NORTH);
deskTop.add(two,BorderLayout.CENTER);
one.setLayout(new BorderLayout());
one.setBackground(Color.red);
two.setBackground(Color.blue);
one.add(msg,BorderLayout.CENTER);
two.setLayout(new FlowLayout());
//窗口关闭事件
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
}); for(int i = 0;i<number.length;i++){
number[i] = new JButton(String.valueOf(i));
number[i].addActionListener(new number_Action());
two.add(number[i]);
}
for (int i = 0; i < operator.length; i++) {
operator[i] = new JButton(operatorValue[i]);
operator[i].addActionListener(new operator_Action());
operator[i].setActionCommand(String.valueOf(i));
two.add(operator[i]);
}
} private class number_Action
implements ActionListener{
public void actionPerformed(ActionEvent ae){
for(int i= 0;i<10;i++){
if(ae.getSource().equals(number[i])){
msg.append(String.valueOf(i));
}
}
}
} private class operator_Action
implements ActionListener{ /**获取第一个参数的值
*
*/
public void getFirstValue(){
first = Integer.parseInt(msg.getText());
msg.setText("");
} public void getSecondValue(){
second = Integer.parseInt(msg.getText());
} public void actionPerformed(ActionEvent ae){
switch (Integer.parseInt(ae.getActionCommand())){
case 0: //+
whichOperator = 0;
getFirstValue();
break;
case 1://-
whichOperator = 1;
getFirstValue();
break;
case 2://*
whichOperator = 2;
getFirstValue();
break;
case 3://"/"
whichOperator = 3;
getFirstValue();
break;
case 4://"="
switch (whichOperator){
case 0://+
getSecondValue();
end = first + second;
System.out.println(end);
break;
/**
* getSecondValue();
end = first + second;
msg.setText(String.valueOf(end));
break;*/
case 1://-
getSecondValue();
end = first - second;
System.out.println(end);
break;
case 2://*
getSecondValue();
end = first * second;
System.out.println(end);
break;
case 3://"/"
getSecondValue();
try {
end = first / second;
System.out.println(end);
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,"除数不能零");
msg.setText("");
}
JOptionPane.showMessageDialog(null,"/");
break;
}
case 5://"清零"
msg.setText("");
break;
}
}
} public static void main(String[] args){
Test sample = new Test();
sample.setSize(new Dimension(300,300));
sample.setVisible(true);
}
}
兄弟帮帮们`看看咋滴了`
第一,不能连续进行计算。你这里的连续计算结果不对。我的这个可以!:)
第二,计算结果不是显示在text里。是显示在DOS窗口里。我的这个显示在test里!:)
第三,有和我的今天中午发现的一样的问题,加减剩除不能多按,否则出错!:)
第四,你的计算器,只要等于了,在按任何符号,都会出错。我的是按了等号后,还可以继续运算。
呵呵,大概了试用了一下,发现了以上的问题。你也试用一下我的,给我提提意见啊。我今天中午搞了半天,才发现,我的计算器,第一次运算的时候,按任何符号,都不会出错,而进行连算第二次的时候,多按一下符号就出错,郁闷死我了。不过你有比我做的好的地方,就是,你的异常处理的比我好。羡慕。呵呵,想起来了,偶还比你多一个小数点功能,哈哈~~~:)
==============================================================================
最后请问一下zrtl(刚从软件园回来)
如果不用Float的话,用什么类型的数据不会出现这种问题呢?
要用异常的话,应该怎么做???最后的相关问题,我明白了就立刻结贴加分。
或者你主动抛出一个new Exception试试。