JAVA流程控制的问题

[复制链接]
查看11 | 回复1 | 2011-2-24 03:21:05 | 显示全部楼层 |阅读模式
有两道题:
第一道:
public class Hello
{
final static int x=0;
public static int y=1;
public static void main(String args[])
{
for(int k=0;k3;k)
{
switch(k)
{

case x:System.out.println(\\\"0\\\");

case y:System.out.println(\\\"1\\\");

case y 1:System.out.println(\\\"2\\\");
}
}
}
}
为什么case y 和case y 1行编译错误

第二道:

public class Hello
{
static boolean b1;
static boolean b2;
public static void main(String args[])
{
int x=1;
if(!b1)
{
if(!b2)
{

b1=true;
}
if(!b1)
{

x=x 10;
}
else if(b2=true)
{

x=x 100;
}
else if(b1|b2)
{

x=x 1000;
}
}
System.out.pringtln(x);
}
}
为什么运行结果是101
回复

使用道具 举报

千问 | 2011-2-24 03:21:05 | 显示全部楼层
<pre id=\\\"best-answer-content\\\" class=\\\"reply-text mb10\\\">第一题case后面接的是常量,y和y 1都是变量,final 修饰的变量就是常量。
第二题boolean类型数据默认值是false,注释1和注释2处得if语句都通过,此时b1为true,b2为false,因此注释3处if语句不通过,注释4处是b2=true而不是b2==true,所以通过,x为101.
public class Hello
{
static boolean b1;
static boolean b2;
public static void main(String args[])
{
int x=1;
if(!b1)//1
{
if(!b2)//2
{

b1=true;
}
if(!b1)//3
{

x=x 10;
}
else if(b2=true)//4
{

x=x 100;
}
else if(b1|b2)
{

x=x 1000;
}
}
System.out.pringtln(x);
}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行