简体   繁体   中英

Show variable in switch statement

switch (name){
    case "Rusia":
       String a="Tuan Rumah"
        break;
    case "Brazil":
        String a="Zona Concacaf"
        break;
}
zona.setText(a);

How to show variable "a" into zona textfield in android studio

You need to define a outside of the switch statement, and then set it inside. For example:

String a = "";
switch (name){
    case "Rusia":
        a = "Tuan Rumah";
        break;
    case "Brazil":
        a = "Zona Concacaf";
        break;
    default:
        a = "Unknown";
}
zona.setText(a);

Define it out the switch block:

    String a = "default";
    switch (name){
        case "Rusia":
            a="Tuan Rumah"
            break;
        case "Brazil":
            a="Zona Concacaf"
            break;
    }
    zona.setText(a);
String a = "";
switch (name){
    case "Rusia":
        a = "Tuan Rumah";
        break;
    case "Brazil":
        a = "Zona Concacaf";
        break;
}
if(!TextUtils.isEmpty(a))
    zona.setText(a);

Define variable 'a' outside of switch statement and before it:

String a ="":
Switch()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM