#点击其他按钮不会出现闪退,本人猜测,可能是算法部分出现问题,我想问问这个算法还有救么。
代码块
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bt1;
Button bt2;
Button bt3;
Button bt4;
Button bt5;
Button bt6;
Button bt7;
Button bt8;
Button bt9;
Button bt0;
Button dot;
Button add;
Button sub;
Button mul;
Button div;
Button equ;
Button clear;
Button del;
private EditText result;
private StringBuilder mystring = new StringBuilder();
private void initView() {
bt0 = (Button) findViewById(R.id.bt0);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
bt6 = (Button) findViewById(R.id.bt6);
bt7 = (Button) findViewById(R.id.bt7);
bt8 = (Button) findViewById(R.id.bt8);
bt9 = (Button) findViewById(R.id.bt9);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
dot = (Button) findViewById(R.id.dot);
clear = (Button) findViewById(R.id.clear);
del = (Button) findViewById(R.id.del);
equ = (Button) findViewById(R.id.equ);
result = findViewById(R.id.result);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt0.setOnClickListener(this);
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
dot.setOnClickListener(this);
equ.setOnClickListener(this);
del.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void onClick(View v) {
char lastchar = ' ';
if (result.getText().toString().length() != 0) {
lastchar = result.getText().toString().charAt(result.getText().toString().length() - 1);
}
switch (v.getId()) {
case R.id.bt0:
mystring = mystring.append('0');
result.setText(mystring);
break;
case R.id.bt1:
mystring = mystring.append('1');
result.setText(mystring);
break;
case R.id.bt2:
mystring = mystring.append('2');
result.setText(mystring);
break;
case R.id.bt3:
mystring = mystring.append('3');
result.setText(mystring);
break;
case R.id.bt4:
mystring = mystring.append('4');
result.setText(mystring);
break;
case R.id.bt5:
mystring = mystring.append('5');
result.setText(mystring);
break;
case R.id.bt6:
mystring = mystring.append('6');
result.setText(mystring);
break;
case R.id.bt7:
mystring = mystring.append('7');
result.setText(mystring);
break;
case R.id.bt8:
mystring = mystring.append('8');
result.setText(mystring);
break;
case R.id.bt9:
mystring = mystring.append('9');
result.setText(mystring);
break;
case R.id.add:
if (lastchar >= '0' && lastchar <= '9') {
mystring = mystring.append('+');
}
result.setText(mystring);
break;
case R.id.sub:
if (lastchar >= '0' && lastchar <= '9') {
mystring = mystring.append('-');
}
result.setText(mystring);
break;
case R.id.mul:
if (lastchar >= '0' && lastchar <= '9') {
mystring = mystring.append('×');
}
result.setText(mystring);
break;
case R.id.div:
if (lastchar >= '0' && lastchar <= '9') {
mystring = mystring.append('÷');
}
result.setText(mystring);
break;
case R.id.dot:
mystring = mystring.append('.');
result.setText(mystring);
break;
case R.id.del:
if (mystring.length() != 0) {
mystring = mystring.delete(mystring.length() - 1, mystring.length());
result.setText(mystring);
}
break;
case R.id.clear:
mystring = mystring.delete(0, mystring.length());
result.setText(mystring);
break;
case R.id.equ:
String mystringx = result.toString();
getResult(mystringx);
break;
default:
break;
}
}
private void getResult(String mystringx) {
double frontNum;
double backNum;
double resultx;
int length = mystringx.length();
for (int i = 0; i <= length - 1; i++) {
if (mystringx.charAt(i) == '×' || mystringx.charAt(i) == '÷') {
int frontNumPlace = frontNamePlace(mystringx, i);
int backNumPlace = backNamePlace(mystringx, i);
frontNum = Double.parseDouble(mystringx.substring(frontNumPlace, i - 1));
backNum = Double.parseDouble(mystringx.substring(i + 1, backNumPlace));
if (mystringx.charAt(i) == '×')
resultx = frontNum * backNum;
else
resultx = 1.0 * frontNum / backNum;
mystringx = change(mystringx, frontNumPlace, backNumPlace, resultx, i);
}
}
for (int i = 0; i <= length - 1; i++) {
if (mystringx.charAt(i) == '+' || mystringx.charAt(i) == '-') {
int frontNumPlace = frontNamePlace(mystringx, i);
int backNumPlace = backNamePlace(mystringx, i);
frontNum = Double.parseDouble(mystringx.substring(frontNumPlace, i - 1));
backNum = Double.parseDouble(mystringx.substring(i + 1, backNumPlace));
if (mystringx.charAt(i) == '+')
resultx = frontNum + backNum;
else
resultx = frontNum - backNum;
mystringx = change(mystringx, frontNumPlace, backNumPlace, resultx, i);
}
}
result.setText(mystringx);
}
private String change(String mystring, int frontNumPlace, int backNumPlace, double result, int i) {
int length = mystring.length();
String mystringy = "";
if (frontNumPlace != 0)
mystringy += mystring.substring(frontNumPlace, i - 1);
mystringy += String.valueOf(result);
if (backNumPlace != length - 1)
mystringy += mystring.substring(i + 1, backNumPlace);
return mystringy;
}
private int frontNamePlace(String mystring, int i) {
int frontNumPlace = 0;
for (int x = i - 1; x > 0; x--) {
if (mystring.charAt(x) == '×' || mystring.charAt(x) == '÷' || mystring.charAt(x) == '+' || mystring.charAt(x) == '-')
frontNumPlace = x + 1;
}
return frontNumPlace;
}
private int backNamePlace(String mystring, int i) {
int length = mystring.length();
int backNumPlace = length - 1;
for (int y = i + 1; y < length - 1; y++) {
if (mystring.charAt(y) == '×' || mystring.charAt(y) == '÷' || mystring.charAt(y) == '+' || mystring.charAt(y) == '-')
backNumPlace = y - 1;
}
return backNumPlace;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/result"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="1"
android:id="@+id/bt1"
android:textSize="40dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="2"
android:id="@+id/bt2"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="3"
android:id="@+id/bt3"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="4"
android:id="@+id/bt4"
android:textSize="40dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="5"
android:id="@+id/bt5"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="6"
android:id="@+id/bt6"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="7"
android:id="@+id/bt7"
android:textSize="40dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="8"
android:id="@+id/bt8"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="9"
android:id="@+id/bt9"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="+"
android:id="@+id/add"
android:textSize="40dp"
android:background="@drawable/btbg2"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="0"
android:id="@+id/bt0"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="—"
android:id="@+id/sub"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="×"
android:id="@+id/mul"
android:textSize="40dp"
android:background="@drawable/btbg2"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="."
android:id="@+id/dot"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg2"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="÷"
android:id="@+id/div"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="T"
android:id="@+id/del"
android:textSize="40dp"
android:background="@drawable/btbg2"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="="
android:id="@+id/equ"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg2"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="C"
android:id="@+id/clear"
android:textSize="40dp"
android:layout_marginLeft="55dp"
android:background="@drawable/btbg2"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
btbg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#2196F3"/>
<stroke android:color="@color/black" android:width="5dp"/>
<corners android:radius="10dp"/>
<padding android:left="10dp" android:bottom="10dp" android:right="10dp" android:top="10dp"/>
</shape>
btbg2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
<stroke android:color="@color/black" android:width="5dp"/>
<corners android:radius="10dp"/>
<padding android:left="10dp" android:bottom="10dp" android:right="10dp" android:top="10dp"/>
</shape>