if语句和switch语句

时间:2023-03-09 18:12:19
if语句和switch语句

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 自动生成的方法存根 } }