为了账号安全,请及时绑定邮箱和手机立即绑定

计算器应用问题 - 开头为 0,数字为点

计算器应用问题 - 开头为 0,数字为点

HUH函数 2021-10-06 12:41:08
我一直在研究 android-studio 计算器。我是一个非常初学者,我有两个问题。首先,当我在我的应用程序中按“c”时,它在第一次点击时工作正常。然后当我点击它并点击任何数字时,它会显示“0x”(x 是那个数字)。我的第二个问题是如何只添加一个点?我尝试使用 String.contains() 但它没有用。我怎样才能解决这个问题?
查看完整描述

2 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

对于您的第一个问题,请执行以下操作:


public void eightNumber(View view) {

    TextView t = (TextView) findViewById(R.id.textView);

    temp = t.getText();

    temp = temp + "8"


    //convert the input number to double to avoid extra zero before digits (05)

    double tempNumber = Double.parseDouble(temp);

    //convert our double number back to string, to use it in textView.setText method

    temp = String.valueOf(tempNumber);


    //to avoid redundant ".0" after integer numbers:

    //if 2 last digits of the number is .0, we delete it

    if(temp.lastIndexOf(".0") == temp.length() - 2){

        temp = temp.substring(0, temp.length() - 2);

    }


    t.setText(temp);

}

以此类推所有 9 位数字。但是你的第二个问题,你只需要颠倒 if 条件:


public void pressDot(View view)

{

    TextView t = (TextView) findViewById(R.id.textView);

    doot=t.toString();


    if (!doot.contains(dooot)) {

        t.append(".");

    }


}

并且不需要 else 块。如果您需要更多帮助,请告诉我。


查看完整回答
反对 回复 2021-10-06
?
侃侃无极

TA贡献2051条经验 获得超10个赞

发现一个错误:


public void pressDot(View view)

{

    TextView t = (TextView) findViewById(R.id.textView);

    doot=t.getText().toString(); //should be this


    if (doot.contains(dooot))t.append(".");

    else t.append("");


}


查看完整回答
反对 回复 2021-10-06
  • 2 回答
  • 0 关注
  • 296 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信