zoukankan      html  css  js  c++  java
  • Java语言基础:语句

    阅读目录:

    1.程序流程控制

      顺序结构

      判断结构

      选择结构

      循环结构

    1.程序流程控制

    程序流程控制:就是程序运行时候的控制。

      顺序结构

    class Demo2
    {
        public static void main(String[] args)
        {
            System.out.println('hello world1')
            System.out.println('hello world2')
            System.out.println('hello world3')
            System.out.println('hello world4')
        }
    }

      判断结构

    格式1:

    class Demo3
    {
        public static void main(String[] args)
        {
            /*
                if 语句的第一种格式:
                (1)
                    if (条件表达式)    //要么真,要么假
                    {
                        执行语句;
                    }
                    
                    例子:
                    int x = 3;
                    if (x>1)
                    {
                        System.out.pritln("yes");
                    }
                    System.out.println("over");
            */
            
        }
    }

    格式2:

    class Demo4
    {
        public static void main(String[] args)
        {
            /*
                if 语句的第二种格式:
                (1)
                    if (条件表达式)    //要么真,要么假
                    {
                        执行语句;
                    }
                    else
                    {
                        执行语句;
                    }
                    System.out.println("hello world");
                    
                    例子:
                    int x = 3;
                    if (x>1)
                    {
                        System.out.pritln("yes");
                    }
                    else
                    {
                        System.out.pritln("no");
                    }
                    System.out.println("over");
                    
                    if (a>1)
                        b=100;
                    else 
                        b=200;
                        
                    b = a>1?100:200; //三元运算符就是if else 语句简写格式。
                    
                    简写格式什么时候使用?
                        当if else 运算后有一个具体的结果时,可以简化写成三元运算符。
                    
            */
            
        }
    }

    格式3:

    class Demo4
    {
        public static void main(String[] args)
        {
            /*
                if 语句的第二种格式:
                (1)
                    if (条件表达式)    //要么真,要么假
                    {
                        执行语句;
                    }
                    else if (条件表达式)  //如果前面为True,就不执行。
                    {
                        执行语句;
                    }
                    else  // 如果前面有一个为True,都不执行。
                    {
                        执行语句;
                    }
                    System.out.println("hello world");
                    
                    例子:
                    int x = 3;
                    if (x>1)
                    {
                        System.out.pritln("a");
                    }
                    else if (x>2)
                    {
                        System.out.pritln("b");
                    }
                    else if (x>3)
                    {
                        System.out.println("c");
                    }
                    else
                    {
                        System.out.println("d");
                    }
                    
                    System.out.println("hello world")
                    
            */
            
        }
    }

    局部代码块:

    class Demo4
    {
        public static void main(String[] args)
        {
            { // 局部代码块可以定义局部变量的生命周期
                int a = 3;
                System.out.rpintln(a+4);
            }
        }
    }

    练习:

    class Demo5
    {
        public static void main(String[] args)
        {
            /*
                需求:根据用户指定的具体数据,判断该数据对应的星期。
                1-星期一Monday
                
                思路:
                    用户输入无法获取,但是那只是具体数据的一种获取手段而且。
                    而我们要做的功能仅仅是对用户指定的数据进行对应星期的打印而且。
                    所以具体的数据不确定,完全可以使用变量来表示。
                    我们只要对变量进行操作即可,至于变量的值,可以由用户决定。
                    因为数据的不确定性,所以要对数据进行判断。
                    使用if语句
            */
            if week;
            if(week==1)
                System.out.println(week+"对应中文星期一")
            else if(week==2)
                System.out.println(week+"对应中文星期二")
            else if(week==3)
                System.out.println(week+"对应中文星期三")
            else if(week==4)
                System.out.println(week+"对应中文星期四")
            else if(week==5)
                System.out.println(week+"对应中文星期五")
            else if(week==6)
                System.out.println(week+"对应中文星期六")
            else if(week==7)
                System.out.println(week+"对应中文星期日")
            else
                System.out.println(week+"没有对应的星期")
        }
    }
    class Demo6
    {
        public static void main(String[] args)
        {
            /*
                一年四季:
                春:3,4,5
                夏:6,7,8
                秋:9,10,11
                冬:12,1,2
                根据用户输入的月份,给出对应的季节。
            */
            int month;
            if (month==3 || month==4 || month==5)
                System.out.println(month+"月是春季");
            else if(month==6 || month==7 || month==8)
                System.out.println(month+"月是夏季");
            else if(month==9 || month==10 || month==11)
                System.out.println(month+"月是秋季");
            else if(month==12 || month==1 || month==2)
                System.out.println(month+"月是冬季");            
            else:
                System.out.println(month+"月没有对应的季节");
                
                
                
            int month = 13;
            if(month<1 || month>12)
                System.out.println(month+"月没有对应的季节");    
            else if(month>=3 & month<=5)
                System.out.println(month+"月是春季")
            else if(month>=6 & month<=8)
                System.out.println(month+"月是夏季")            
            else if(month>=9 & month<=11)
                System.out.println(month+"月是秋季")            
            else 
                System.out.println(month+"月是冬季")            
                        
        }
    }


      选择结构

    class SwitchDemo
    {
        public static void main(String[] args)
        {
            /*
            选择
            switch(表达式)
            {
                case 取值1:
                    执行语句;
                    break;
                case 取值2:
                    执行语句;
                    break;
                ...
                default:
                    执行语句;
                    break;
            }
            */
            
            int x = 3
            Switch(x) //byte,short,int,char.
            {
                case 4:
                    System.out.println("a");
                    break;
                case 1:
                    System.out.println("b");
                    break;        
                case 3:
                    System.out.println("c");
                    break;    
                default:
                    System.out.println("d");
                    break;                    
            }
            System.out.println('hello world')
            
            
            int a=4, b=2,
            char opr = '-';
            switch(opr)
            {
                case '+':
                    System.out.println(a+b);
                    break;
                case '-':
                    System.out.println(a-b);
                    break;                
                case '*':
                    System.out.println(a*b);
                    break;                
                case '/':
                    System.out.println(a/b);
                    break;            
                default:
                    System.out.println('无法运算,符号不支持');
                    break;                
    
            }
            
        }
    }

    练习:

    class SwitchTest
    {
        public static void main(String[] args)
        {
            /*
                需求:用户输入的数据对应出星期
            */
            int week = 1;
            switch(week):
            {
                case 1:
                    System.out.println(week+"对应的是星期一");
                    break;
                case 2:
                    System.out.println(week+"对应的是星期二");
                    break;
                case 3:
                    System.out.println(week+"对应的是星期三");
                    break;
                case 4:
                    System.out.println(week+"对应的是星期四");
                    break;
                case 5:
                    System.out.println(week+"对应的是星期五");
                    break;
                case 6:
                    System.out.println(week+"对应的是星期六");
                    break;
                case 7:
                    System.out.println(week+"对应的是星期七");
                    break;                
                default:
                    System.opr.println('没有对应的星期');
                    
                    
                int month = 5;
                switch(month)
                {
                    case 3:
                    case 4:
                    case 5:
                        System.opr.println(month+"月对应的是春季");
                        break;
                    case 6:
                    case 7:
                    case 8:
                        System.opr.println(month+"月对应的是夏季");
                        break;
                    case 9:
                    case 10:
                    case 11:
                        System.opr.println(month+"月对应的是秋季");
                        break;
                    case 12:
                    case 1:
                    case 2:
                        System.opr.println(month+"月对应的是冬季");
                        break;                
                    default:
                        System.opr.println('没有对应的季节');
                }
    
            }
        }
    }

    小结:

    判断(if)  和 选择(Switch) 的区别:
        if1.对具体的值进行判断。
            2.对区间判断。
            3.对运算结果是boolean类型的表达式进行判断。
        
        Switch:
            1.对具体的值进行判断。
            2.值的个数通常是固定的。
            对于几个固定的值判断,建议使用switch语句,因为switch语句会将具体的答案都加载进内存。
            效率相对高一点。

      循环结构

    循环结构:
        代表语句: whiledo whilefor
        1.while语句格式:
            while (条件表达式)
            {
                执行语句;
            }
            
        2.do while语句格式:
            do
            {
                执行语句;
            }while(条件格式);
            
        do while 的特点: 是条件无论是否满足,循环体至少执行一次。

    while:

    class WhileDemo
    {
        public static void main(String []  args)
        {
            /*
                while(条件表达式)
                {
                    执行语句;
                }
            */
            
            int x = 1;
            while (x<3)
            {
                System.out.println("x="+x);
                x++;
            }
            
            System.out.println('hello world')
        }
    }

    练习:

    class WhileTest
    {
        public static void main(String[] args)
        {
            /*
                练习:
                1.获取1到10的数字之和。
                    0 + 1
                        1 + 2
                            3 + 3
                                6 + 4
                                    10 + 5
                思路:    
                    1.每次参与加法的数值不确定。
                    2.每次的出现的和数据也不确定。
                    3.发现参与加法运算的数值有递增规律
                    4.每一次都是加法运算在重复,并且都是和再加上下一个数值。
                    
                步骤:
                    1.定义一个变量,记录住参与加法运算的数据。
                    2.定义一个变量,记录中每一次的出现的和。
                    3.对于记录参与加法运算的数据进行递增。
                    4.因为加法运算需要重复,就要想到循环结构。
            */
            // 累加算法。
            int x = 1; //记录参与加法的数据
            int sum = 0; //记录住每一次的和
            while(x<=10)
            {
                sum  = sum + x;
                x++;
                
            }
            System.out.println("sum="+sum);
            
            
        }
    }

    do while:

    class DoWhileDemo
    {
        public static void main(String []  args)
        {
            /*
                do
                {
                    执行语句;
                }while(条件表达式);
            */
            
            int x = 1;
            do 
            {
                System.out.println("x="+x);
                x++;
            }
            while (x<3);
            System.out.println('hello world')
            
            /*
                do while 语句的特点:无论条件是否满足,循环体至少执行一次
            */
            
            int y = 1;
            while (y<1)
            {
                System.out.println("y="+y);
                x++;
            }
            
        }
    }

    练习:

    class WhileTest2
    {
        public static void main(String[] args)
        {
            /*
                练习:
                1.1-100之间,6的倍数出现的次数。
                要求:自己独立完成思想的书写,和步骤的文字描述。
                
                1.定义一个变量,记住每次的数值。
                2.定义一个变量,记住次数
                
                
            */
            //计数器思想
            int x = 1;    // 目标数
            int count = 0;  //次数
            while(x <=100)
            {
                if(x%6==0)
                    count++;
                x++;
            }
            System.out.println("count="+count)
            
        }
        
    }

     For:

    class ForDemo
    {
        public static void (String[] args)
        {
            /*
                for(初始化表达式;循环条件表达式;循环后的操作表达式)
                {
                    执行语句;(循环体)
                }
                
                执行顺序:
                    1.先执行初始化表达式
                    2.后执行循环条件表达式
                    3.执行语句
                    4.执行操作表达式
            */
            
            for(int x = 1; x<3; x++)
            {
                System.out.println("x="+x);
            }
            
        }
    }

    练习:

    class ForDemo
    {
        public static void(String[] args)
        {
            /*
                用for完成累加。
            */
            int sum = 0;
            for(int x=1; x<=0; x++)
            {
                sum  = sum + x;
            }
            System.out.println("sum="+sum)
            
            forwhile 的特点:
                1.for和while可以互换。
                2.格式上的不同,在使用上有点小区别。
                    如果需要通过变量来对循环进行控制,该变量只作为循环增量存在时,区别就体现出来了、
            
            //打印1-10十个数字
            int x =1;
            while(x<5)
            {
                System.out.println("x="+x);
                x++;
            }
            System.out.println("x=="+x);   //可执行
            
            for(int y=1; y<5; y++)
            {
                System.out.println("y="+y);
                
            }
            System.out.println("y=="+y);  //报错 ,因为for循环,在循环里结束时就释放内存空间了。
            
            
        }
        
    }
    问题:
        1.什么时候使用循环结构呢?
            答:当对某些代码执行很多次时,使用循环结构完成。
                当对一个条件进行一次判断时,可以使用if语句。
                当对一个条件进行多次判断时,可以使用while语句。
                
                注意:
                1.在使用循环时,一定要明确哪些语句需要参与循环。
                2.循环通常情况下,需要定义条件,需要控制次数

    For循环的嵌套:

    //for循环的嵌套结构。(大圈套小圈思想,外面1圈,里面4圈。)
    class ForForDemo
    {
        public static void main(String[] args)
        {
            for(int x=0; x<3; x++)
            {
                for(int y=0; y<4; y++;)
                {
                    System.out.println("ok")
                }
            }
        }
    }            
    
    例子:
    
    class ForForDemo
    {
        public static void main(String[] args)
        {
            /*
                *****
                *****
                *****
                *****
            */
            for(int x=0; x<4; x++)   //有四行
            {
                for(int y=0; y<5; y++;)  //每一行有5个*
                {
                    System.out.print("*")
                }
                System.out.println();
            }
        }
    }

    例子2:

    class ForForDemo2
    {
        public static void main(String[] args)
        {
            /*
                *****
                ****
                ***
                **
                *
            */
            int z = 5;
            for(int x=1; x<=5; x++)   //1-5,1-4,1-3 // 1-5,2-5,3-5
            {
                for(int y=1; y<=z; y++;)  //每一行有5个*
                {
                    System.out.print("*")
                }
                System.out.println();
                z--;
            }
        }
    }    
    
    
    class ForForDemo2
    {
        public static void main(String[] args)
        {
            /*
                *****
                ****
                ***
                **
                *
            */
            int z = 1;
            for(int x=1; x<=5; x++)   //1-5,1-4,1-3 尾变 // 1-5,2-5,3-5 头变
            {
                for(int y=z; y<=5; y++;)  //每一行有5个*
                {
                    System.out.print("*")
                }
                System.out.println();
                z++;
            }
        }
    }

    例子3:

    class ForForDemo2
    {
        public static void main(String[] args)
        {
            /*
                *
                **
                ***
                ****
                *****
            */
            for(int x=1; x<=5; x++)   
            {
                for(int y=1; y<=x; y++;)  
                {
                    System.out.print("*")
                }
                System.out.println();
            
            }
        }
    }    
    
    class ForForDemo2
    {
        public static void main(String[] args)
        {
            /*
                54321
                5432
                543
                54
                5
            */
            for(int x=1; x<=5; x++)   
            {
                for(int y=5; y>=x; y--;)  
                {
                    System.out.print("y")
                }
                System.out.println();
            
            }
        }
    }
    
    class ForForDemo2
    {
        public static void main(String[] args)
        {
            /*
                1
                22
                333
                4444
                55555
            */
            for(int x=1; x<=5; x++)   
            {
                for(int y=1; y<=x; y++;)  
                {
                    System.out.print("x")
                }
                System.out.println();
            
            }
        }
    }

    例子4:

    class ForForDemo3
    {
        public static void main(String[] args)
        {
            /*
                九九乘法表
                1*1=1
                1*2=2 2*2=4
                1*3=3 2*3=6 3*3=9
                
                
     : 回车
                	 :制表符
                 : 退格
                
     :按下回车键
            */
            for(int x=1; x<=9; x++)   
            {
                for(int y=1; y<=x; y++;)  
                {
                    System.out.print(y+"*"+x+"="+y*x+"")
                }
                System.out.println();
            
            }
        }
    }

    例子5:

    class ForForDemo4
    {
        public static void main(String[] args)
        {
            /*
                * * * * *
                -* * * *
                --* * *
                ---* *
                ----*
            */
            for(int x=1; x<=5; x++)
            {
                for(int y=1;y<x; y++)
                {
                    System.out.print(" ")
                }
                for(int z=x; z<=5; z++)
                {
                    System.out.print("* ")
                }
                
                System.out.println();
            }
            
        }
    }

    break&continue

    其他流程控制语句:
    1.break(跳出)  , continue(继续)
        break语句: 应用范围:选择结构和循环结构
        continue语句: 应用于循环结构
    注意:    
        a.这两个语句离开应用范围,存在是没有意义的。
        b.这两个语句单独存在下面都不可以有语句,因为执行不到。
        c.continue 语句是结束本次循环继续下次循环。
        d. 标号的出现,可以让这两个语句作用于指定的范围。

    例子:

    class BreakContinueDemo01
    {
        public static void main(String[] args)
        {
            /*
                break:跳出
                break作用的范围,要么是switch,要么是循环语句。
            */
            for(int x=0; x<3; x++)
            {
                System.out.println("x="+x);
                break;
            }
        }
    }
    
    class BreakContinueDemo02
    {
        public static void main(String[] args)
        {
            /*
                break:跳出
                break作用的范围,要么是switch,要么是循环语句。
                break跳出所在的当前循环。
                如果出现嵌套循环,break想要跳出指定的循环,可以通过标号来完成。
            */
            asd:for(int x=0; x<3; x++)
            {
                zxc:for (int y=0; y<4; y++)
                {
                    System.out.println("x="+x);
                    break asd;
                }
    
            }
        }
    }
    
    class BreakContinueDemo03
    {
        public static void main(String[] args)
        {
            /*
                continue:继续
                作用的范围:循环结构
                continue: 结束本次循环,继续下次循环
                如果continue单独存在时,下面不要有任何语句,因为执行不到。
                
                
                for (int x=0; x<11; x++)
                {
                    if(x%2==0)
                        continue;
                    System.out.println("x="+x);
                }
                
            */
            asd:for(int x=0; x<3; x++)
            {
                zxc:for (int y=0; y<4; y++)
                {
                    System.out.println("x="+x);
                    continue asd;
                }
    
            }
        }
    }
  • 相关阅读:
    [C语言] 交换排序之冒泡排序的特性及实现
    [C语言] 选择排序之鸡尾酒排序的特性及实现
    [C语言] 选择排序之直接选择排序的特性及实现
    计蒜客 蓝桥杯模拟 快速过河
    计蒜客 蓝桥杯模拟 瞬间移动 dp
    计蒜客 蓝桥杯模拟 充话费
    计蒜客 蓝桥杯模拟二 区间合并 打扫教室
    商品类目短文本分类总结
    SpringBoot项目创建及入门基础
    Joyful HDU
  • 原文地址:https://www.cnblogs.com/zhongbokun/p/10508256.html
Copyright © 2011-2022 走看看