gpt4 book ai didi

java - 安卓工作室 : error: incompatible types: double cannot be converted to String

转载 作者:行者123 更新时间:2023-11-30 10:19:14 24 4
gpt4 key购买 nike

我是 android studio 的新手,正在尝试制作一个计算器应用程序。我的几乎所有按钮都可以使用(0-9),但我只需要对加法、减法、乘法和除法按钮进行编程。现在我遇到了一个问题:

error: incompatible types: double cannot be converted to String.

我真的不知道如何改变它让它工作。这是我的代码的一部分:

//Button A (Addition) Event Handler
buttonA.setOnClickListener(
//Button A Interface
new Button.OnClickListener(){
//Button A Callback Method
public void onClick(View v){
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString());
output.setText("");
sign = "+";
}
}
);

//Button S (Subtract) Event Handler
buttonS.setOnClickListener(
//Button S Interface
new Button.OnClickListener(){
//Button S Callback Method
public void onClick(View v){
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString());
output.setText("");
sign = "-";
}
}
);

//Button D (Divide) Event Handler
buttonD.setOnClickListener(
//Button D Interface
new Button.OnClickListener(){
//Button D Callback Method
public void onClick(View v){
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString());
output.setText("");
sign = "/";
}
}
);

//Button M (Multiply) Event Handler
buttonM.setOnClickListener(
//Button M Interface
new Button.OnClickListener(){
//Button M Callback Method
public void onClick(View v){
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString());
output.setText("");
sign = "*";
}
}
);

//Button Equals
buttonE.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
TextView output = (TextView)findViewById(R.id.editText);
tempDouble2 = Double.parseDouble(output.getText().toString());

if (sign .equals("+")){
output.setText(Double.toString(tempDouble + tempDouble2));
}
else if (sign .equals("-")){
output.setText(Double.toString(tempDouble - tempDouble2));
}
else if (sign .equals("X"))){
output.setText(Double.toString(tempDouble * tempDouble2));
}
else if (sign .equals("/")){
if (tempDouble2 == 0){
//Cannot devide by zero
output.setText("X");
}
else {
output.setText(Double.toString(tempDouble / tempDouble2));
}
}

//Reset the Sign variable
sign = "";
}
}
);
}

我也得到了错误:

error: bad operand types for binary operator '-'

error: bad operand types for binary operator '*'

error: bad operand types for binary operator '/'

有经验的人可以帮帮我吗?

最佳答案

您不能在两个 Double 对象之间使用“+”运算符。

如果你想将 Double 转换为 String,你应该使用

String.valueOf(tempDouble);

如果你想添加两个 Double 对象,你应该使用

tempDouble.doubleValue() + tempDouble2;

关于java - 安卓工作室 : error: incompatible types: double cannot be converted to String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731777/

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