相关疑难解决方法(0)

开关盒中的变量范围

我想我不明白示波器如何在开关盒中工作.

有人可以向我解释为什么第一个代码不编译但第二个代码没有编译?

代码1:

 int key = 2;
 switch (key) {
 case 1:
      String str = "1";
      return str;
 case 2:
      String str = "2"; // duplicate declaration of "str" according to Eclipse.
      return str;
 }
Run Code Online (Sandbox Code Playgroud)

代码2:

 int key = 2;
 if (key == 1) {
      String str = "1";
      return str;
 } else if (key == 2) {
      String str = "2";
      return str;
 }
Run Code Online (Sandbox Code Playgroud)

为什么变量"str"的范围不包含在案例1中?

如果我跳过案例1的声明,则永远不会声明变量"str"...

java switch-statement

91
推荐指数
3
解决办法
3万
查看次数

标签 统计

java ×1

switch-statement ×1