[英]Changing switch statement to if statement
I want to change this switch statement to if statement or while loop. 我想将此switch语句更改为if语句或while循环。 How can I do that?
我怎样才能做到这一点? Note this piece of code using Android Studio.
注意使用Android Studio的这段代码。
public void onClick(View v) {
switch(v.getId()){
case R.id.button:
Log.v(TAG, "verbose");
break;
case R.id.button2:
Log.d(TAG, "Debug");
break;
case R.id.button3:
Log.i(TAG, "Information");
break;
case R.id.button4:
Log.w(TAG, "Warning");
break;
case R.id.button5:
Log.e(TAG,"Error");
break;
}
}
I would suggest you to use existing switch
statement, but if you really want if
, you can do it like this: 我建议您使用现有的
switch
语句,但是如果您确实想要if
,可以这样做:
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button) {
Log.v(TAG, "Verbose");
} else if (id == R.id.button2) {
Log.d(TAG, "Debug");
} else if (id == R.id.button3) {
Log.i(TAG, "Information");
} else if (id == R.id.button4) {
Log.w(TAG, "Warning");
} else if (id == R.id.button5) {
Log.e(TAG, "Error");
}
}
If you're using AndroidStudio, you could use intent 如果您使用的是AndroidStudio,则可以使用Intent
switch
keyword switch
关键字上 alt+enter
(on Mac, not sure about Windows) alt+enter
(在Mac上,不确定Windows) Replace 'switch' with 'if'
Replace 'switch' with 'if'
Note: You could replace if to switch using the same method. 注意:如果要使用相同的方法进行切换,则可以替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.