您的位置:首页 > 其它

if语句和switch语句

2015-12-03 15:25 197 查看
1.基本写法

if

if(逻辑表达式){语句;}else if{语句;else{语句;}

switch

switch(变量){case 常量值:语句;break;default:语句;}

2.举例

if else语句

public class ccc {

/**
* @param args
*/
public static void main(String[] args) {

int a = 10

if(a>18)
{
System.out.println("你已经成年");
}
else if(a=18)
{
System.out.println("你刚满十八岁");
}
else
{
System.out.println("你还未成年");
}
//

// TODO 自动生成的方法存根

}

}


2.switch语句

public class ccc {

/**
* @param args
*/
public static void main(String[] args) {
int a = 5;
switch(a)
{
case 0:
System.out.println("你是个好人");
break;
case 5:
System.out.println("你不是好人");
break;
default:
System.out.println("输入有误");
break;
}
// TODO 自动生成的方法存根

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: