日期:2014-05-20  浏览次数:20684 次

java的移位运算的一个小问题
public class HomeWork4 {
public static void main(String[] args) {
 
int schedule = 0x77; 
String[] flag = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
Scanner in = new Scanner(System.in);
System.out.print("请输入你要查询的星期: ");
String week;

int shift = 0;
while(true){
week = in.nextLine();

for(shift = 0 ; shift<=6;shift++){
if(week==flag[shift])
break;
}
if(shift>6){
System.out.print("您的输入是错误的,请重新输入:");
}
}
   
  // 下面一步编译不通过。什么原因。该如何解决
int result = (schedule >> shift) & 0x01;
if(1 == result)
System.out.println("有航班!!!");

else
System.out.println("对不起。没有航班!!!!");
}

}
  编译时在int result = (schedule >> shift) & 0x01; 这一步报错是什么原因。怎么解决。


------解决方案--------------------
编译通不过的原因是因为你之前写了个死循环:

while(true) {

而且这个循环里面根本没有 break; 这种跳出循环的语句。
——注意,for循环中的break 只能跳出for循环本身,不能跳出while循环!!


所以编译器认为后续的代码全都不可能被执行到,是“dead code”。