码辣烫 2021-09-22 10:28 采纳率: 0%
浏览 44

【安卓开发计算器】为什么点击‘=’按钮时虚拟机会退出

#点击其他按钮不会出现闪退,本人猜测,可能是算法部分出现问题,我想问问这个算法还有救么。

img

代码块

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>

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-09-22 10:40
    关注

    看下是否有报错,闪退说明程序有异常。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月22日

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)