修复了一些bug,实现了基本的加减运算数据:使用double类型存储数字,定义一个x表示字符串现在应有的长度,当t改变时使用x调整t的长度,x最高为15
方法:新建一个字符串,用户按按钮时对其进行改变
如:t=“0 ”
用户按1,+,2键
t=“1+2 ”
遍历一遍t将数字与符号分别存入两个数组中,之后再进行运算
监听代码:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("7")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0') {
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'7');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("8")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'8');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("9")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'9');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("4")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'4');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("5")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'5');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("6")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'6');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("1")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'1');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("2")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;
}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'2');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("3")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'3');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}else if(e.getActionCommand().equals("0")) {
if(x==15) {
return;
}else if(t.charAt(0)=='0'){
return;
}else {
if(t.charAt(x-1)=='+') {
o=1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'0');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;
}
}else if(e.getActionCommand().equals("+")) {
if(x==15||x==14) {
return;
}else if(t.charAt(0)=='0'||s==1) {
return;
}else {
if(y==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'+');
strBuilder.toString();
t=strBuilder.toString();
s=1;
y=0;
x=x+1;
}
}else if(e.getActionCommand().equals("-")) {
if(x==15||x==14) {
return;
}else if(t.charAt(0)=='0'||y==1) {
return;
}else
{
if(s==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'-');
strBuilder.toString();
t=strBuilder.toString();
y=1;
s=0;
x=x+1;
}
}else if(e.getActionCommand().equals("=")) {
int j=0;
double []a=new double[10];
int b=0;
String[] t2=new String[10];
while(t.charAt(j)!=' ') {
String t1="0 ";
for(int i=j;i<16;i++) {
if(t.charAt(i)>='0'&&t.charAt(i)<='9') {
StringBuilder strBuilder1 = new StringBuilder(t1);
strBuilder1.setCharAt(i-j,t.charAt(i));
strBuilder1.toString();
t1=strBuilder1.toString();
}else {
c[b]=t.charAt(i);
j=i+1;
break;
}
}
double all=0;
for(int g=0;g<t1.replace(" ","").length();g++) {
double u=(int)Math.pow(10,t1.replace(" ","").length()-g-1);
all=all+u*Integer.parseInt(t1.charAt(g)+"");
}
t2[b]=t1;
a[b]=all;
b=b+1;
}
if(sum==0) {
sum=a[0];
}
for(int l=0;l<b;l++) {
if(c[l]=='+') {
sum=sum+a[l+1];
}else if(c[l]=='-') {
sum=sum-a[l+1];
}
}
int f=0;
for(int m=0;m<b;m++) {
if(t2[m].replace(" ","").length()>10) {
t="error";
sum=0;
f=1;
}
}
if(f==0) {
t=String.valueOf(df.format(sum));
}
x=t.replace(" ", "").length();
for(int h=0;h<16-(x-1);h++) {
t=t+" ";
}
s=0;y=0;
}else if(e.getActionCommand().equals("Replay")) {
t="0 ";
x=1;
sum=0;
}
}