码迷,mamicode.com
首页 > 其他好文 > 详细

if语句和switch语句

时间:2015-12-03 15:35:47      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

    }

}

 

if语句和switch语句

标签:

原文地址:http://www.cnblogs.com/Chenshuai7/p/5016378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!