Switch 语句无法识别字段变量
为什么我的代码在 b.Equal 处切换不识别“displayValue”。所有其他开关均可识别显示屏。是因为它是开关中的开关吗?请告诉我。
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
////////////////////////////////////////////////////////////
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
// Here's display
public EditText display;
double total1=0.0;
double total2=0.0;
char theOperator;
public String buttonText;
public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// display
display= (EditText) findViewById(R.id.editText1);
}
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
String display1= display.getText().toString();
double displayValue= Double.parseDouble(display1);
total1+=displayValue;
display.setText("");
}
// All the switches recognize and use display... except for Equals
public void onClick(View v) {
switch(v.getId()){
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
display.setText("");
break;
case R.id.bAdd:
buttonText="+";
ButtonAdd= (Button)findViewById(R.id.bAdd);
ButtonAdd.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bEqual:
switch (theOperator){
case '+':
//Error right here. This switch doesn't recognize displayValue,
// but the other switches recognize display. Why?
total2= total1 + displayValue;
}
}
}
}
Why doesn't my code switch at b.Equal recognize "displayValue". All the other switches recognize display. Is it because it's a switch in a switch? Please let me know.
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
////////////////////////////////////////////////////////////
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
// Here's display
public EditText display;
double total1=0.0;
double total2=0.0;
char theOperator;
public String buttonText;
public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// display
display= (EditText) findViewById(R.id.editText1);
}
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
String display1= display.getText().toString();
double displayValue= Double.parseDouble(display1);
total1+=displayValue;
display.setText("");
}
// All the switches recognize and use display... except for Equals
public void onClick(View v) {
switch(v.getId()){
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
display.setText("");
break;
case R.id.bAdd:
buttonText="+";
ButtonAdd= (Button)findViewById(R.id.bAdd);
ButtonAdd.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bEqual:
switch (theOperator){
case '+':
//Error right here. This switch doesn't recognize displayValue,
// but the other switches recognize display. Why?
total2= total1 + displayValue;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与开关位于另一个开关内无关。事实上,在 onClick() 方法中,甚至不存在像 displayValue 这样的变量。其他开关识别显示,因为显示是该类的成员变量。他们也无法识别displayValue,因为displayValue不是该类的成员变量。其实开关里面的开关也能识别显示。
我认为您将“显示”与“显示值”混淆了。它们是不同的变量。
This has got nothing to do with switch being inside another switch. The fact is that within the onClick() method, no such variable as displayValue even exists. The other switches recognize display, because display is a member variable of the class. They would not be able to recognize displayValue either, because displayValue is not a member variable of the class. In fact, the switch inside the switch would also be able to recognize display.
I think you are confusing "display" with "displayValue". They are different variables.
displayValue 超出了 onClick 方法的范围。将 displayValue 更改为全局变量..
displayValue is out of scope for the method onClick. Change displayValue as a global variable..