if-else 在 android 中的字符串的 for 循环中不起作用

if-else not working in for loop for string in android

我正在尝试制作一个计算器。但是我为 num2 赋值的条件不起作用。无法弄清楚代码有什么问题。

MainActivity.java

    package rihan.calc3;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnLongClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener {

        Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnadd, btnsub, btnmul, btndiv, btndot, btneq,
        btnclr;
        RelativeLayout rel;
        TextView txt;

        int dot = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn1 = (Button) findViewById(R.id.btn1);
            btn2 = (Button) findViewById(R.id.btn2);
            btn3 = (Button) findViewById(R.id.btn3);
            btn4 = (Button) findViewById(R.id.btn4);
            btn5 = (Button) findViewById(R.id.btn5);
            btn6 = (Button) findViewById(R.id.btn6);
            btn7 = (Button) findViewById(R.id.btn7);
            btn8 = (Button) findViewById(R.id.btn8);
            btn9 = (Button) findViewById(R.id.btn9);
            btn0 = (Button) findViewById(R.id.btn0);
            btnadd = (Button) findViewById(R.id.btnadd);
            btnsub = (Button) findViewById(R.id.btnsub);
            btnmul = (Button) findViewById(R.id.btnmul);
            btndiv = (Button) findViewById(R.id.btndiv);
            btndot = (Button) findViewById(R.id.btndot);
            btneq = (Button) findViewById(R.id.btneq);
            btnclr = (Button) findViewById(R.id.btnclr);
            rel = (RelativeLayout) findViewById(R.id.rel);
            txt = (TextView) findViewById(R.id.txt);

            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
            btn6.setOnClickListener(this);
            btn7.setOnClickListener(this);
            btn8.setOnClickListener(this);
            btn9.setOnClickListener(this);
            btn0.setOnClickListener(this);
            btnadd.setOnClickListener(this);
            btnsub.setOnClickListener(this);
            btnmul.setOnClickListener(this);
            btndiv.setOnClickListener(this);
            btndot.setOnClickListener(this);
            btneq.setOnClickListener(this);
            btnclr.setOnClickListener(this);

            btnclr.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub

                    return false;
                }
            });
        }

        @SuppressWarnings("null")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
            case R.id.btn1:
                txt.append("1");
                break;

            case R.id.btn2:
                txt.append("2");
                break;

            case R.id.btn3:
                txt.append("3");
                break;

            case R.id.btn4:
                txt.append("4");
                break;

            case R.id.btn5:
                txt.append("5");
                break;

            case R.id.btn6:
                txt.append("6");
                break;

            case R.id.btn7:
                txt.append("7");
                break;

            case R.id.btn8:
                txt.append("8");
                break;

            case R.id.btn9:
                txt.append("9");
                break;

            case R.id.btn0:
                txt.append("0");
                break;

            case R.id.btnadd:
                txt.append(" + ");
                dot = 0;
                break;

            case R.id.btnsub:
                txt.append(" - ");
                dot = 0;
                break;

            case R.id.btndiv:
                txt.append(" / ");
                dot = 0;
                break;

            case R.id.btnmul:
                txt.append(" x ");
                dot = 0;
                break;

            case R.id.btneq:
                String[] txt_arr = txt.getText().toString().split(" ");
                String symbol = "i";

                boolean symbol_flag = false;
                int array_index = 0;

                float num1 = 0, num2 = 0, result = 0;

                for (array_index = 0; array_index < txt_arr.length - 1; array_index++) {
                    if (txt_arr[array_index].contains("+")) {
                        symbol = txt_arr[array_index];
                        symbol_flag = true;
                    }

                    else {
                        if (symbol_flag == true) {
                            num2 = Float.parseFloat(txt_arr[array_index]);
                            result = num1 + num2;
                            num1 = result;
                        }

                        else {
                            num1 = Float.parseFloat(txt_arr[array_index]);
                        }
                    }
                }

                Toast.makeText(MainActivity.this,
                "result : " + result + ". num1 : " + num1 + ". Symbol is : " + symbol + ". Symbol flag is : "
                        + symbol_flag +". num2 : " + num2 + ". length is : " + txt_arr.length,
                Toast.LENGTH_SHORT).show();
                // array_index=0;
                break;

            case R.id.btnclr:

                break;

            case R.id.btndot:
                break;

            default:
                break;
            }

        }

    }

没有对 num2 执行任何操作。 IF 条件未得到处理。然而它的其他条件工作正常

                    **else {
                        if (symbol_flag == true) {
                            num2 = Float.parseFloat(txt_arr[array_index]);
                            result = num1 + num2;
                            num1 = result;
                        }**

                        else {
                            num1 = Float.parseFloat(txt_arr[array_index]);
                        }
                    }

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/background_dark" >

        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:maxLength="25"
            android:text="0"
            android:textAlignment="textEnd"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:background="@android:color/background_dark"
            android:text="1"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button        
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn1"
            android:background="@android:color/background_dark"
            android:text="2"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn2"
            android:background="@android:color/background_dark"
            android:text="3"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnadd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn3"
            android:background="@android:color/background_dark"
            android:text="+"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn1"
            android:background="@android:color/background_dark"
            android:text="4"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn2"
            android:layout_toRightOf="@id/btn4"
            android:background="@android:color/background_dark"
            android:text="5"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn3"
            android:layout_toRightOf="@id/btn5"
            android:background="@android:color/background_dark"
            android:text="6"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnadd"
            android:layout_toRightOf="@id/btn6"
            android:background="@android:color/background_dark"
            android:text="-"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn4"
            android:background="@android:color/background_dark"
            android:text="7"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn5"
            android:layout_toRightOf="@id/btn7"
            android:background="@android:color/background_dark"
            android:text="8"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn6"
            android:layout_toRightOf="@id/btn8"
            android:background="@android:color/background_dark"
            android:text="9"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnmul"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnsub"
            android:layout_toRightOf="@id/btn9"
            android:background="@android:color/background_dark"
            android:text="x"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btndot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn7"
            android:background="@android:color/background_dark"
            android:text="."
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn8"
            android:layout_toRightOf="@id/btndot"
            android:background="@android:color/background_dark"
            android:text="0"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnclr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn9"
            android:layout_toRightOf="@id/btn0"
            android:background="@android:color/background_dark"
            android:text="C"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btndiv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnmul"
            android:layout_toRightOf="@id/btnclr"
            android:background="@android:color/background_dark"
            android:text="/"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btneq"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btndot"
            android:background="@android:color/background_dark"
            android:text="="
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btneq"
            android:maxLength="25"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

    </RelativeLayout>

txt1 没有用。请忽略它。

请告诉我如果条件在双星号内有什么问题

    for (j = 0; j < str.length; j++) {
                    if (str[j].equals("+")) {
                        symbol=str[j];
                    }
                    else if (str[j].equals("-")){
                        symbol=str[j];
                    }
                    else if (str[j].equals("x")){
                        symbol=str[j];
                    }
                    else if (str[j].equals("/")){
                        symbol=str[j];
                    }

                    else{
                        if(symbol=="i")
                            num1 = Float.parseFloat(str[j]);
                        else{
                            num2 = Float.parseFloat(str[j]);
                            if (str[j].equals("+")) {
                                result=num1+num2;
                            }
                            else if (str[j].equals("-")){
                                result=num1-num2;
                            }
                            else if (str[j].equals("x")){
                                result=num1*num2;
                            }
                            else if (str[j].equals("/")){
                                result=num1/num2;
                            }
                        }
                    }

仍然无法正常工作。 :(

因为 toast 显示符号标志值 = true 那么它肯定得到它的值。

已更新

        String[] txt_arr = txt.getText().toString().split(" ");
        String symbol = "i";

        boolean symbol_flag = false;
        int array_index = 0;

        float num1 = 0, num2 = 0, result = 0;

        for (array_index = 0; array_index < txt_arr.length; array_index++) {
            if (txt_arr[array_index].equals("+") || txt_arr[array_index].equals("-")
                    || txt_arr[array_index].equals("x") || txt_arr[array_index].equals("/")) {
                symbol = txt_arr[array_index];
                symbol_flag = true;
            }

            else {
                if (symbol_flag == true) {

                    num2 = Float.parseFloat(txt_arr[array_index]);
                    if (txt_arr[array_index].equals("+")) {
                        result = num1 + num2;
                        num1 = result;
                    }

                    else if (txt_arr[array_index].equals("-")) {
                        result = num1 - num2;
                        num1 = result;
                    }

                    else if (txt_arr[array_index].equals("x")) {
                        result = num1 * num2;
                        num1 = result;
                    }

                    else if (txt_arr[array_index].equals("/")) {
                        result = num1 / num2;
                        num1 = result;
                    }
                }

                else {
                    num1 = Float.parseFloat(txt_arr[array_index]);
                }
            }
        }

        Toast.makeText(MainActivity.this,
                "result : " + result + ". num1 : " + num1 + ". Symbol is : " + symbol + ". Symbol flag is : "
                        + symbol_flag + ". num2 : " + num2 + ". length is : " + txt_arr.length,
                Toast.LENGTH_SHORT).show();

如果未达到 "if",则永远不会设置 "symbol"。你确定

if (txt_arr[array_index].contains("+")) 

按预期工作? “.equals”不是更清楚吗?

[编辑] 此外,symbol_flag == true 的测试未在设置它的同一迭代中完成。中间有一个"else"。在设置符号标志后,您应该再循环一次 运行 。

for (array_index = 0 ; array_index < txt_arr.length ; array_index++) {