你的位置:首页 > Java教程

[Java教程]break 千万不能忘


public class client(

  public static void main(String [] argw){

    System.out.println("2 = "+numCase(2));

}

 

public static String numCase(int n){

  String ChinessNumber="";

  swith(n){

  case 0:ChinessNumber="零";

  case 0:ChinessNumber="壹";

  case 0:ChinessNumber="贰";

  case 0:ChinessNumber="叁";

  case 0:ChinessNumber="肆";

  case 0:ChinessNumber="伍";

  case 0:ChinessNumber="陆";

  case 0:ChinessNumber="柒";

  case 0:ChinessNumber="捌";

  case 0:ChinessNumber="玖";

  }

  return ChinessNumber;

}

)

 

 

结果 是 2=玖

 

case语句后边一定要加上break;

可以这样解决:修改IDE的警告级别,例如在Eclipse中,可以以Performaces-java-compiler-Errors/Warnings-Potential Programming problems,然后修改‘swith’ case fall-through 这Errors级别,如果你胆敢不在case语句中加入break,那么Eclipse直接就报个红叉给你看看