![](/img/trans.png)
[英]error: incompatible types: double cannot be converted to double[]
[英]Android studio: error: incompatible types: double cannot be converted to String
我是 android studio 的新手,正在嘗試制作一個計算器應用程序。 幾乎我所有的按鈕都在工作(0-9),但我只需要對加法、減法、乘法和除法按鈕進行編程。 現在我遇到了一個問題:
錯誤:不兼容的類型:double 無法轉換為 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 = "";
}
}
);
}
我也收到錯誤:
錯誤:二元運算符“-”的錯誤操作數類型
錯誤:二元運算符“*”的錯誤操作數類型
錯誤:二元運算符“/”的錯誤操作數類型
請有經驗的人幫幫我好嗎?
不能在兩個Double
對象之間使用“+”運算符。
如果要將 Double 轉換為 String,則應使用
String.valueOf(tempDouble);
如果你想添加兩個 Double 對象,你應該使用
tempDouble.doubleValue() + tempDouble2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.