猿问

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

我一直在研究 android-studio 计算器。我是一个非常初学者,我有两个问题。首先,当我在我的应用程序中按“c”时,它在第一次点击时工作正常。然后当我点击它并点击任何数字时,它会显示“0x”(x 是那个数字)。

我的第二个问题是如何只添加一个点?我尝试使用 String.contains() 但它没有用。我怎样才能解决这个问题?


HUH函数
浏览 297回答 2
2回答

MMTTMM

对于您的第一个问题,请执行以下操作: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 块。如果您需要更多帮助,请告诉我。

侃侃无极

发现一个错误: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("");}
随时随地看视频慕课网APP

相关分类

Java
我要回答