首页 > 解决方案 > 方法 endsWith() 在 android java 编码中不起作用

问题描述

我正在用 android 创建一个简单的计算器。我正在实施一个解决方案,如果用户在数字末尾按下任何运算符并按下“BtnEqual”,它将修剪任何数学运算符(如果存在于字符串末尾)。该字符串是从 TEXTVIEW 变量 Tv 中检索的。我的问题是,即使我使用了 endsWith() 方法,它也会从字符串的末尾修剪任何字符,即使它不是数学运算符。稍后我将在“BtnEqual”上实现数学运算,但我被困在这里,因为这个问题。以下是 MainActivity.java 文件的内容:

package org.betaapps.simplecalculator;

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

public class MainActivity extends Activity implements OnClickListener {
TextView Tv;String Disp;

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

    Toast.makeText(this, "Welcome :)", Toast.LENGTH_SHORT).show();
    int[]Btn_Id={ 
        R.id.Btn0, R.id.Btn1, R.id.Btn2, R.id.Btn3, R.id.Btn4, R.id.Btn5,
        R.id.Btn6, R.id.Btn7, R.id.Btn8, R.id.Btn9, R.id.BtnClear, R.id.BtnDecimal,
        R.id.BtnDivide, R.id.BtnMultiply, R.id.BtnSubtract, R.id.BtnAdd, R.id.BtnEqual
    };
    for(int i : Btn_Id){
        ((Button)findViewById(i)).setOnClickListener(this);
    }
    Tv=(TextView)findViewById(R.id.txtScreen);

}

@Override
public void onClick(View v) {
    Disp=Tv.getText().toString();
    switch(v.getId())
    {
        case R.id.Btn0:
            if(Disp.equals("0")) {                      //If user pressed 0 and screen is 0
                Tv.setText("0");
            }
            else {                                          //If screen contains other numbers, then append 
                Tv.append("0");
            }
            break;
        case R.id.Btn1:
        case R.id.Btn2:
        case R.id.Btn3:
        case R.id.Btn4:
        case R.id.Btn5:
        case R.id.Btn6:
        case R.id.Btn7:
        case R.id.Btn8:
        case R.id.Btn9:
            if(Disp.equals("0")) {                  //If user presses digits and screen has 0,replaces
                Tv.setText(((Button)v).getText().toString());
            }
            else {                                                      //If screen contains other than 0
                Tv.append(((Button)v).getText().toString());
            }
            break;


        case R.id.BtnAdd:                                               //If operator is pressed after an operator, toast warning
            if(Disp.endsWith("+") || Disp.endsWith("-") || Disp.endsWith("*") || Disp.endsWith("/")) {                          //If user presses operator after an operator
                Toast.makeText(this, "+ not allowed at this point!", Toast.LENGTH_SHORT).show();
            }
            else {
                Tv.append("+");                                     //last character is number, so append
            }
            break;
        case R.id.BtnSubtract:
            if(Disp.endsWith("+") || Disp.endsWith("-") || Disp.endsWith("*") || Disp.endsWith("/")) {                          //If user presses operator after an operator
                Toast.makeText(this, "- not allowed at this point!", Toast.LENGTH_SHORT).show();
            }
            else {
                Tv.append("-");
            }
            break;
        case R.id.BtnMultiply:
            if(Disp.endsWith("+") || Disp.endsWith("-") || Disp.endsWith("*") || Disp.endsWith("/")) {                          //If user presses operator after an operator
                Toast.makeText(this, "* not allowed at this point!", Toast.LENGTH_SHORT).show();
            }
            else {
                Tv.append("*");
            }
            break;
        case R.id.BtnDivide:
            if(Disp.endsWith("+") || Disp.endsWith("-") || Disp.endsWith("*") || Disp.endsWith("/")) {                          //If user presses operator after an operator
                Toast.makeText(this, "÷ not allowed at this point!", Toast.LENGTH_SHORT).show();
            }
            else {
                Tv.append("/");
            }
            break;  
        case R.id.BtnClear:
            Tv.setText("0");//Everything will be 0 again
            break;


        case R.id.BtnEqual:
            if(Tv.getText().toString().endsWith("+") || Tv.getText().toString().endsWith("-") || Tv.getText().toString().endsWith("*") || Tv.getText().toString().endsWith("/"));
            {
                Tv.setText(Tv.getText().toString().substring(0, (Tv.getText().toString().length()-1)));
                //Removing last operator
            }
            break;
        }
    }
}

标签: javaandroid

解决方案


问题是您的语句后的分号 ( ;) 。if

你的代码:

if (condition);
{
     // trim character
}

trim character代码块不受条件约束,无论如何if都会被执行。

它应该是:

if (condition)
{
    // trim character
}

推荐阅读