方法endsWith()在android java编码中不起作用

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

我正在用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;
        }
    }
}

问题在于
if
语句后面的分号(

您的代码:

if (condition);
{
     // trim character
}
trim character
代码块将不受
if
条件的约束,并且将被执行

应该是:

if (condition)
{
    // trim character
}

您可以为此编写简单的java代码,将字符串作为param传递并返回o/p字符串。为这种敏感类型编写单元测试logic@jiteshmohite你能详细说明一下或者给我举个小例子吗?我是android新手,只学了一些基础知识。@RobbyCornelissen我稍后将在代码中实现数学逻辑,我现在面临的主要问题是,如果用户按下“BtnEqual”按钮,将删除最后一个操作符。非常感谢!你的解决方案非常有效。很抱歉,我之前没有注意到代码中的语法错误:)