Java学习日记:Week1---day4-5 条件结构和循环结构句式

  Java中的条件结构语法跟循环结构语法跟c语言中的是一模一样的。

  这里就针对上次发的c语言中的做一些补充。

1.if..

  例1:

  这里之前用的是switch穿透达到累加的效果算出的

  现在利用if句式的独立性达到累加效果

  另外需要特别注意的是  这里的if如果换成else if就不成立了,因为else..if句式中如果一个if 或者else if中的语句成立就不会去判断其他的else if,因此在使用else if句式是应该尽量保证条件相互独立, 不要使上面的条件包含下面的条件  否则会出现意料之外的结果。

import java.util.Scanner;

public class Homework3
{
    public static void main(String[] args)
    {
        //3.根据用户输入的年份,月份,日期计算该日期是一年中的第几天?
        Scanner input = new Scanner(System.in);
        int year,month,day,i;
        int total = 0;
        
        System.out.println("请输入年份");
        year = input.nextInt();
        System.out.println("请输入月份");
        month = input.nextInt();
        System.out.println("请输入日期");
        day = input.nextInt();
        
        //判断闰年
        if(year%4==0&&year%100!=0||year%400==0)
        {
            i = 1;
        }
        else
        {
            i = 0;
        }
        if(month<=12)
            total += 30;
        if(month<=11)
            total += 31;
        if(month<=10)
            total += 30;
        if(month<=9)
            total += 31;
        if(month<=8)
            total += 31;
        if(month<=7)
            total += 30;
        if(month<=6)
            total += 31;
        if(month<=5)
            total += 30;
        if(month<=4)
            total += 31;
        if(month<=3)
            total += 28 + i;
        if(month<=2)
            total += 31;
        if(month<=1)
            total += day;
        
        /*
        switch(month)
        {
            case 12:
            total += 30;
            case 11:
            total += 31;
            case 10:
            total += 30;
            case 9:
            total += 31;
            case 8:
            total += 31;
            case 7:
            total += 30;
            case 6:
            total += 31;
            case 5:
            total += 30;
            case 4:
            total += 31;
            case 3:
            total += 28 + i;
            case 2:
            total += 31;
            case 1:
            total += day;
        }
        */
        
        System.out.println(year+"年"+month+"月"+day+"日是当年的第"+total+"天");
    }
}

2.switch

  switch语句的穿透效果在default以及break位置不同时的不同效果

  例2:

import java.util.Scanner;

public class TestSwitch1
{
    public static void main(String[] args)
    {    
        //switch 语句case,default穿透测试
        Scanner input = new Scanner(System.in);
        
        System.out.print("请输入数字:");
        int num = input.nextInt();
        
        switch(num)
        {
            case 1:
                System.out.println("pass1");
            case 2:
                System.out.println("pass2");
            case 3:
                System.out.println("pass3");
                break;
            case 4:
                System.out.println("pass4");
                break;
            default:
                System.out.println("passdefault");
        }
        
        System.out.println("==========================================");
        
        switch(num)
        {
            case 1:
                System.out.println("pass1");
            case 2:
                System.out.println("pass2");
            case 3:
                System.out.println("pass3");
            case 4:
                System.out.println("pass4");
            default:
                System.out.println("passdefault");
        }
        
        System.out.println("==========================================");
        
        switch(num)
        {    
            default:
                System.out.println("passdefault");
            case 1:
                System.out.println("pass1");
            case 2:
                System.out.println("pass2");
            case 3:
                System.out.println("pass3");
            case 4:
                System.out.println("pass4");
        }
        
        System.out.println("==========================================");
        
        switch(num)
        {    
            default:
                System.out.println("passdefault");
                break;
            case 1:
                System.out.println("pass1");
            case 2:
                System.out.println("pass2");
            case 3:
                System.out.println("pass3");
            case 4:
                System.out.println("pass4");
        }
        
        System.out.println("==========================================");
        
        switch(num)
        {    
            case 1:
                System.out.println("pass1");
            default:
                System.out.println("passdefault");
            case 2:
                System.out.println("pass2");
            case 3:
                System.out.println("pass3");
            case 4:
                System.out.println("pass4");
        }
    }
}

    上面程序输入1之后输出结果如下:

    输入4运行结果如下:

    从这里可以看出 default的位置并不会影响其他case的穿透与判断

3. 循环

  没什么好补充的,在使用多重循环时注意内层循环跟外形循环参数的关系就行了;

转载于:https://www.cnblogs.com/Z-o-Y/p/6665319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值