这可能是史上最小白的计算器吧,自己写的差点把自己给绕晕了

这可能是史上最小白的计算器吧,自己写的差点把自己给绕晕了

各位带上防闪镜再看,哈哈

package awt_AnNiu;





import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;




public class Test {


public static void main(String[] args) {
//声明一个整型一个字符类型字符串,用来存放输入的数据
int [] arr;
arr=new int[100];
char [] acc;
acc=new char[100];


Frame f=new Frame("计算器");
///取消窗口布局
f.setLayout(null);
//设置窗口大小
f.setSize(600, 500);
//设置窗口位置
f.setLocation(300,200);
//窗口大小不可拉伸
f.setResizable(false);
//窗口可见性
f.setVisible(true);
//窗口可关闭
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
Window window=(Window)e.getComponent();
window.dispose();

}
});

///计算结果
Label new1=new Label("计算结果→");
new1.setSize(60, 20);
new1.setLocation(10,35);
f.add(new1);

///文本框
TextField tf=new TextField();
//位置
tf.setLocation(80,30);
//长宽
tf.setSize(300,30);
//添加按钮
f.add(tf);
Button btn0=new Button("0");
Button btn1=new Button("1");
Button btn4=new Button("4");
Button btn7=new Button("7");
Button btn2=new Button("2");
Button btn5=new Button("5");
Button btn8=new Button("8");
Button btn3=new Button("3");
Button btn6=new Button("6");
Button btn9=new Button("9");
Button btnAdd=new Button("+");
Button btnReduce=new Button("-");
Button btnMultiplied=new Button("*");
Button btnExcept=new Button("÷");
Button btnReset=new Button("重置");
Button btnYes=new Button("确认");

btn0.setSize(100,60);
btn1.setSize(100,60);
btn2.setSize(100,60);
btn3.setSize(100,60);
btn4.setSize(100,60);
btn5.setSize(100,60);
btn6.setSize(100,60);
btn7.setSize(100,60);
btn8.setSize(100,60);
btn9.setSize(100,60);
btnAdd.setSize(100,60);
btnReduce.setSize(100,60);
btnMultiplied.setSize(100,60);
btnExcept.setSize(100,60);
btnReset.setSize(100,60);
btnYes.setSize(100,60);

btn0.setLocation(115, 275);
btn1.setLocation(10, 80);
btn4.setLocation(10, 145);
btn7.setLocation(10, 210);
btn2.setLocation(115, 80);
btn5.setLocation(115, 145);
btn8.setLocation(115, 210);
btn3.setLocation(220, 80);
btn6.setLocation(220, 145);
btn9.setLocation(220, 210);
btnAdd.setLocation(325,80);
btnReduce.setLocation(325,145);
btnMultiplied.setLocation(325,210);
btnExcept.setLocation(430,80);
btnReset.setLocation(430,145);
btnYes.setLocation(430, 210);

f.add(btn0);
f.add(btn1);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn1==e.getSource()) {
String str=btn1.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=1;
acc[i]='1';
break;
}
}
}


}
}

);
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.add(btn5);
f.add(btn6);
f.add(btn7);
f.add(btn8);
f.add(btn9);
f.add(btnAdd);
f.add(btnReduce);
f.add(btnMultiplied);
f.add(btnExcept);
f.add(btnReset);
f.add(btnYes);

btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn2==e.getSource()) {
String str=btn2.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=2;
acc[i]='1';
break;
}
}


}
}

});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn3==e.getSource()) {
String str=btn3.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=3;
acc[i]='1';
break;
}
}


}
}

});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn4==e.getSource()) {
String str=btn4.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=4;
acc[i]='1';
break;
}
}


}
}

});
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn5==e.getSource()) {
String str=btn5.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=5;
acc[i]='1';
break;
}
}


}
}

});
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn6==e.getSource()) {
String str=btn6.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=6;
acc[i]='1';
break;
}
}


}
}

});
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn7==e.getSource()) {
String str=btn7.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=7;
acc[i]='1';
break;
}
}


}
}

});
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn8==e.getSource()) {
String str=btn8.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=8;
acc[i]='1';
break;
}
}


}
}

});
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn9==e.getSource()) {
String str=btn9.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=9;
acc[i]='1';
break;
}
}


}
}

});
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn0==e.getSource()) {
String str=btn0.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=0;
acc[i]='1';
break;
}
}
}


}
}

);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnAdd==e.getSource()) {
String str=btnAdd.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
acc[i]='+';
break;
}
}

}
}

});
btnReduce.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnReduce==e.getSource()) {
String str=btnReduce.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
acc[i]='-';
break;
}
}


}
}

});
btnMultiplied.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnMultiplied==e.getSource()) {
String str=btnMultiplied.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
acc[i]='*';
break;
}
}

}
}

});
btnExcept.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnExcept==e.getSource()) {
String str=btnExcept.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
acc[i]='/';
break;
}
}

}
}

});
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnReset==e.getSource()) {
for(int i=0;i<100;i++) {
arr[i]=0;
acc[i]='\0';
}
//将文本框清零
tf.setText(null);


}
}

});
//确认后求和,差,积,商
btnYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int [] sum1,sum2;
sum1=new int [10];
sum2=new int [10];

int wei1=0,wei2=0;
long suma=0,sumb=0;
char c = 0;
int  sum12=0;//当为1时跳转
for(int i=0;i<100;i++) {


if(acc[i]=='+'||acc[i]=='-'||acc[i]=='*'||acc[i]=='/') {
sum12=1;
}

if(acc[i]=='1'&&sum12==0) {
System.out.print(arr[i]);
sum1[wei1]=arr[i];
wei1++;
}
if(acc[i]=='1'&&sum12==1) {
System.out.print(arr[i]);
sum2[wei2]=arr[i];
wei2++;
}


if(acc[i]!='\0'&&acc[i]!='1') {
System.out.print(acc[i]);
c=acc[i];
}



}
System.out.println();
for(int m=0;m<wei1;m++) {
System.out.print(sum1[m]);
suma=(long) (suma+Math.pow(10, wei1-m-1)*sum1[m]);

}
System.out.println();
for(int n=0;n<wei2;n++) {
System.out.print(sum2[n]);
sumb=(long) (sumb+Math.pow(10, wei2-n-1)*sum2[n]);

}
System.out.println();
System.out.println(c);
//计算!
long sumc=0;
if(c=='+') {
sumc=suma+sumb;
}else if(c=='-') {
sumc=suma-sumb;
}else if(c=='*') {
sumc=suma*sumb;
}else if(c=='/') {
sumc=suma/sumb;
}
//将长整型的sumc转换为字符串s
String s=String.valueOf(sumc);
//输出至文本框
tf.setText(s);
//最后重置保存数值的数组
for(int i=0;i<100;i++) {
arr[i]=0;
acc[i]='\0';
}

}


});


}



}

刚学JAVA半个学期,最近学到了图形界面,老师推荐自己写个计算器来熟悉下,所以就认真写啦!

不过这可能是史上最小白的计算器吧,自己写的差点把自己给绕晕了,写了两天时间,实际在电脑面前坐了十五个小时左右,看书,百度搜索,还有自己想的逗逼计算方法,O(∩_∩)O不知道给老师看了会不会打死我

过几天有空在学习优化下算法什么的吧,看别人都是两百行代码就行了,写的比我功能还多一些,还要完善,真的厉害

第一次发博客,记录下自己学习的过程吧!

(ง •_•)ง加油努力!

收获环节:

1.在网上搜到了Java整型与字符串相互转换的方法,还好有这个方法,不然我15个小时还真不一定能写出计算器来,啊啊啊,谢谢写这个博客的大神。

2. 如何在JAVA窗口中显示一段文字?也是在网上找到的。。。

在程序中new一个Label,记得在无布局时写上文字的坐标,方位!

F不是固定的值!  

     Label new1=new Label("计算结果");

     new1.setSize(60, 20);

     new1.setLocation(10,35);  

     f.add(new1);

3.算是有点熟悉set,get方法的大概意思了吧,之前一直懵懵懂懂的


下面是疑问环节:

1.在一个窗体里面可以使用多个布局吗?具体怎么使用,哎,百度看有Panel类可以?我还是一脸懵逼哦

2.这段代码我在程序里面写了好多次,重复太多了,有什么办法可以更简练呢?

btn2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if(btn2==e.getSource()) {
String str=btn2.getLabel();
tf.setText(str);
for(int i=0;i<100;i++) {
if(acc[i]=='\0') {
arr[i]=2;
acc[i]='1';
break;
}

}

3.我的计算器还是不能实现一个两位以上的整数输入后能够显示两位数,只能点击一个按钮,显示一个按钮上的数字,坐的屁股都疼了,过几天再想把,希望不要忘了,哈哈



猜你喜欢

转载自blog.csdn.net/weixin_42377176/article/details/80560201
今日推荐