gpt4 book ai didi

android - 计算器一直显示0

转载 作者:行者123 更新时间:2023-11-30 03:13:47 24 4
gpt4 key购买 nike

我已经在我的应用程序中放入了一个计算器,但出于某种原因,无论我做什么,第一个数字始终为零,所以如果我单击 1,它会显示 01,它不会为结果执行此操作,而是显示带有 .0 的结果最后,这些对我来说都是问题,因为我绝对是一个初学者,需要这个计算器尽可能简单。

我读到 .0 是一个“双重”问题,但我不知道如何摆脱这两个问题中的任何一个。任何人都可以为我阐明这一点吗?这是我的代码。

public class CalcActivity extends Activity implements OnClickListener, OnInitListener {
private EditText Scr;
private float NumberBf;
private String Operation;
private ButtonClickListener btnClick;
private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
Scr = (EditText) findViewById(R.id.editText);
Scr.setEnabled(false);
btnClick = new ButtonClickListener();
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
myTTS= new TextToSpeech(this,this);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);


int idList[] =
{R.id.button0,R.id.button1,R.id.button2,R.id.button3,
R.id.button4,
R.id.button5,R.id.button6,R.id.button7,
R.id.button8,R.id.button9,R.id.buttonDot,
R.id.buttonAdd,R.id.buttonSub,R.id.buttonMul,
R.id. buttonDiv,R.id.buttonEq,R.id.buttonC
};

for(int id:idList){
View v =(View) findViewById(id);
v.setOnClickListener(btnClick);
}
}




public void mMath(String str){
NumberBf = Float.parseFloat(Scr.getText().toString());
Operation = str;
Scr.setText("0");
}
public void getKeyboard(String str){
String ScrCurrent = Scr.getText().toString();
ScrCurrent += str;
Scr.setText(ScrCurrent);

}
public void mResult() {
float NumAf = Float.parseFloat(Scr.getText().toString());
float result = 0;
if(Operation.equals("+")){
result = NumAf + NumberBf;
}
if(Operation.equals("-")){
result = NumberBf - NumAf;
}
if(Operation.equals("*")){
result = NumAf * NumberBf;
}
if(Operation.equals("/")){
result = NumberBf / NumAf;
}
Scr.setText(String.valueOf(result));
}

public void onClick(View v) {
//get the text entered
EditText enteredText = (EditText)findViewById(R.id.editText);
String words = enteredText.getText().toString();
speakWords(words);
}
private void speakWords(String speech) {
//speak straight away
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
public void onInit(int initStatus) {
//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",

Toast.LENGTH_LONG).show();
}
}



private class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonC:
Scr.setText("0");
NumberBf = 0;
Operation = "";
break;
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonSub:
mMath("-");
break;
case R.id.buttonMul:
mMath("*");
break;
case R.id.buttonDiv:
mMath("/");
break;
case R.id.buttonEq:
mResult();
break;
default:
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
break;

}
}
}
}

最佳答案

关于最后的 .0 问题,您可以在调用 setText() 之前使用省略最后两个字符的结果子字符串;也就是说,如果 str 是您用于通过 Scr.settext 方法分配的字符串变量在调用 setText 之前放置这行代码:

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

因此,您最终可以解决 .0 问题。

关于android - 计算器一直显示0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20493292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com