• Java流程控制语句


    基础知识学习基于基础的程序:


    public class Test
    {
        public static void main (String[] args)
        {
            int a=1;
            int b=2;
            
            int c=a>2?3:4;//三元表达式,示例a?b:c;代表如果a为true那么b,反之c;
            
            System.out.println(c);
            /*
                if流程
            */

            if(a>b)
            {
                System.out.println("a>b");
            }
            else if(a<b)
            {
                System.out.println("a<b");
            }
            else
            {
                System.out.println("a==b");
            }
            /*
                switch流程,几乎全部可以被if语句代替,我自身用的机会比较少
            */

            switch (a)//switch 后跟的变量一般为整型或者字符型;
            {
                case 0:
            
                System.out.println("case0");
                
                break;
                case 1:
                
                System.out.println("case1");
                
                break;
                case 2:
                
                System.out.println("case2");
                
                break;
                default:
                    
                System.out.println("default");
                
                break;
            }
            /*
                while循环
            */

            int d=1;
            int sum=0;//先判断在执行
            while(d<=50)
            {
                sum+=d;
                d++;
            }
            System.out.println(sum);
            /*
                do while 可能我经验不足,我放弃了它
            */

            int e=1;
            int suum=0;
            do
            {
                suum+=e;
                e++;
            }
            while(e<=50);//先执行一次,再判断,至少执行一次
            System.out.println(suum);
            /*
                for 循环,这个用的巨多
            */

            int ssm=0;
            for(int i=0;i<=50;i++)
            {
                ssm+=i;    
            }

            System.out.println(ssm);

           /*

                 Continue,跳出循环

           */

           for(int i = 0; i < 10; i++)
            {
                if(5 == i)
                {
                    continue;
                }
                        
                System.out.print(i+",");
            }

        }
        

    }


    结果如下:

    4
    a<b
    case1
    1275
    1275
    1275

    0,1,2,3,4,6,7,8,9,


    对我个人而言:if,for用得比较多,switch用的较少,其他的几乎不用。


  • 相关阅读:
    springmvc乱码问题
    51nod 还是01串
    51nod 1276 岛屿的数量
    poj 2486 a apple tree
    hdu 1011 Starship Troopers
    poj 1155 TELE
    hdu 4586 Play the Dice
    hdu 5023 A Corrupt Mayor's Performance Art(线段树水题)
    Appleman and Tree
    hdu 4003
  • 原文地址:https://www.cnblogs.com/MedivhQ/p/4074999.html
Copyright © 2020-2023  润新知