if(e.getSource()==buttondeng){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
value1=Float.parseFloat(s1);
value2=Float.parseFloat(s2);
switch(c){
case '+':
result=value1+value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '-':
result=value1-value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '*':
result=value1*value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '/':
result=value1/value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case 'z':textfield.setText(textfield.getText());break;
default:break;
}
如题,这是我做的一个非常简易的计算器,为什么我的运算结果小数点后面运算不了
下面是全部代码可以运行的。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
class clwindow extends Frame
implements ActionListener{
double value1=0,value2=0,result=0;
char c='z';
boolean b=false;
private int m=1,n=1,t=1,x=5,y=6;
String s1,s2,s3;
Box basebox,boxV1,boxV2,boxV3,boxV4,boxV5;
Panel panel1,panel2,panel3,panel4,panel5;
TextField textfield;
Button clear,button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,buttondeng,buttonjia,buttonjian,buttoncheng,buttonchu,buttond;
GridLayout grid1,grid2;
clwindow(){
grid1=new GridLayout(1,3);
grid2=new GridLayout(1,5);
panel1=new Panel();
panel2=new Panel();
panel2.setLayout(grid1);
panel3=new Panel();
panel3.setLayout(grid1);
panel4=new Panel();
panel4.setLayout(grid1);
panel5=new Panel();
panel5.setLayout(grid2);
textfield=new TextField(15);
clear=new Button("清除");
buttond=new Button(".");
button0=new Button("0");
button1=new Button("1");
button2=new Button("2");
button3=new Button("3");
button4=new Button("4");
button5=new Button("5");
button6=new Button("6");
button7=new Button("7");
button8=new Button("8");
button9=new Button("9");
buttonjia=new Button("+");
buttonjian=new Button("-");
buttoncheng=new Button("*");
buttonchu=new Button("/");
buttondeng=new Button("=");
boxV1=Box.createVerticalBox();
panel1.add(textfield);
panel1.add(buttondeng);
panel1.add(clear);
boxV1.add(panel1);
boxV2=Box.createVerticalBox();
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
boxV2.add(panel2);
boxV3=Box.createVerticalBox();
panel3.add(button4);
panel3.add(button5);
panel3.add(button6);
boxV3.add(panel3);
boxV4=Box.createVerticalBox();
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
boxV4.add(panel4);
boxV5=Box.createVerticalBox();
panel5.add(button0);
panel5.add(buttonjia);
panel5.add(buttonjian);
panel5.add(buttoncheng);
panel5.add(buttonchu);
panel5.add(buttond);
boxV5.add(panel5);
basebox=Box.createVerticalBox();
basebox.add(boxV1);
basebox.add(boxV2);
basebox.add(boxV3);
basebox.add(boxV4);
basebox.add(boxV5);
add(basebox);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttond.addActionListener(this);
buttonjia.addActionListener(this);
buttonjian.addActionListener(this);
buttoncheng.addActionListener(this);
buttonchu.addActionListener(this);
buttondeng.addActionListener(this);
clear.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
validate();
setBounds(120,125,240,180);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"1");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"1");
s2=textfield.getText();
}
}
if(e.getSource()==button2){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"2");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"2");
s2=textfield.getText();
}
}
if(e.getSource()==button3){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"3");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"3");
s2=textfield.getText();
}
}
if(e.getSource()==button4){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"4");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"4");
s2=textfield.getText();
}
}
if(e.getSource()==button5){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"5");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"5");
s2=textfield.getText();
}
}
if(e.getSource()==button6){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"6");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"6");
s2=textfield.getText();
}
}
if(e.getSource()==button7){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"7");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"7");
s2=textfield.getText();
}
}
if(e.getSource()==button8){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"8");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"8");
s2=textfield.getText();
}
}
if(e.getSource()==button9){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"9");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"9");
s2=textfield.getText();
}
}
if(e.getSource()==button0){
if(t==2&&m==2)textfield.setText(null);
m=1;
if(b==false){
textfield.setText(textfield.getText()+"0");
s1=textfield.getText();
}
if(b==true){
textfield.setText(textfield.getText()+"0");
s2=textfield.getText();
}
}
if(e.getSource()==buttond){
if(n==1){
textfield.setText(textfield.getText()+".");
s1=textfield.getText();
n=2;
}
}
if(e.getSource()==buttonjia){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
textfield.setText(null);
b=true;
c='+';
n=1;
}
if(e.getSource()==buttonjian){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
textfield.setText(null);
b=true;
c='-';
n=1;
}
if(e.getSource()==buttoncheng){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
textfield.setText(null);
b=true;
c='*';
n=1;
}
if(e.getSource()==buttonchu){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
textfield.setText(null);
b=true;
c='/';
n=1;
}
if(e.getSource()==buttondeng){
if(b==true&&m==1&&n==1&&t==1&&c=='z'){}
value1=Float.parseFloat(s1);
value2=Float.parseFloat(s2);
switch(c){
case '+':
result=value1+value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '-':
result=value1-value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '*':
result=value1*value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case '/':
result=value1/value2;s3=String.valueOf(result);textfield.setText(s3);c='z';b=false;n=1;t=2;m=2;break;
case 'z':textfield.setText(textfield.getText());break;
default:break;
}
}
if(e.getSource()==clear){
textfield.setText(null);
value1=0;
value2=0;
b=false;
m=1;
n=1;
t=1;
c='z';
}
}
}
public class calculatortest{
public static void main(String args[]){
new clwindow();
}
}
4 个解决方案
#1
#2
不知道是我浏览器问题,还怎么地,好像楼主的代码关键字都翻译成中文了。。。。。。。。。
#3
果断是你浏览器的问题
#4
为什么不用bigdecimal
#1
#2
不知道是我浏览器问题,还怎么地,好像楼主的代码关键字都翻译成中文了。。。。。。。。。
#3
果断是你浏览器的问题
#4
为什么不用bigdecimal