3位运算_流程控制语句

3位运算_流程控制语句

导语:

想写在写

  1. BreakDemo
  2. ContinueDemo
  3. DoWhileDemo
  4. ForDemo
  5. ForForDemo
  6. ForForTest
  7. ForTest
  8. HexTest
  9. IfDemo
  10. IfTest
  11. OperateDemo4
  12. OperateDemo5
  13. OperateTest
  14. SwitchDemo
  15. SwitchTest
  16. WhileDemo
  17. WhileTest
  18. Test

1. BreakDemo

class BreakDemo 
    {
        public static void main(String[] args) 
        {
            /*
            break:使用范围:switch 循环语句。
            */
            /*
            for (int x=0; x<3 ;x++ )
            {
                if(x==1)
                    break;
                System.out.println("x="+x);            
            }
            */
            outer:for (int x=0; x<3 ;x++ )
            {
                inner:for (int y=0; y<4 ;y++ )
                {
                    System.out.println("x="+x);
                    break outer;
                }
            }
        }
    } 

2. ContinueDemo

class ContinueDemo 
    {
        public static void main(String[] args) 
        {
            /*
            continue:只作用于循环结构。
    
            结束本次循环,继续下次循环。
            */
            for (int x=0; x<10 ;x++ )
            {
                if(x%2==0)
                    continue;
                System.out.println("x="+x);
    
            }
    
            outer:for (int x=0; x<3 ;x++ )
            {
                inner:for (int y=0; y<4 ;y++ )
                {
                    System.out.println("x="+x);
                    continue outer;
                }
            }
    
        }
    } 

3. DoWhileDemo

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

4. ForDemo

class ForDemo 
    {
        public static void main(String[] args) 
        {
    
            /*
            for(初始化表达式;循环条件表达式;循环后的操作表达式)
            {
                执行语句;(循环体)
            }
    
            */
            for(int x = 1; x<3; x++)
            {
                System.out.println("x="+x);
            }
            //System.out.println("x......"+x);
    
            //和while的区别。
            int y = 1;
            while(y<3)
            {
                System.out.println("y="+y);
                y++;
            }
            System.out.println("y....."+y);
    
    
            //最简单的无限循环,
            //while(true){}   for(; ;){}
    
    
        }
    } 

5. ForForDemo

class ForForDemo 
    {
        public static void main(String[] args) 
        {
    
            for (int x=0; x<3; x++)//行
            {
                for(int y=0; y<4; y++)//每一行的个数。
                {
                    System.out.print("*");
                }
                System.out.println();//换行。
            }
    
    
    
    
        }
    } 

6. ForForTest

class ForForTest 
    {
        public static void main(String[] args) 
        {
            /*
            需求:在屏幕上显示以下图形。
            *****
            ****
            ***
            **
            *
            思路:
            1,看起图形,发现是由5行的组成。
            2,每一行都有多个星。
            3,有点意思,多行,循环就可以搞定。每到一行,里面有多个星。
            循环内,还需要循环负责每一行的星的个数。
            4,外循环控制行数,内循环控制每一个行的个数。
    
            步骤:
            1,for的嵌套循环。
            */
            /*
            int z = 5;
            for ( int x=1; x<=5 ; x++ )
            {
                for ( int y=1 ; y<=z ; y++) 
                {
                    System.out.print("*");
                }
                z--;
                System.out.println();
            }
            */
    
            //int z = 1;
            for ( int x=1; x<=5 ; x++ )
            {
                for ( int y=x ; y<=5 ; y++)  
                {
                    System.out.print("&");
                }
                //z++;
                System.out.println();
            }
    
            System.out.println("-----------------------");
    
            /*
    
            *
            **
            ***
            ****
            *****
    
            */
    
            for (int x=1; x<=5 ; x++)
            {
                for (int y=1 ;y<=x ;y++ )
                {
                    System.out.print("*");
                }
                System.out.println();
            }
    
    
    
            /*
            需求:要求在屏幕上显示下列内容。
            54321
            5432
            543
            54
            5
    
    
            1*1=1 
            1*2=2 2*2=4
            1*3=3 2*3=6 3*3=9
    
    
            */
        }
    } 

7. ForTest

class ForTest 
    {
        public static void main(String[] args) 
        {
    
            /*
            需求:获取1-100之间6的倍数的个数。
    
            思路:
            1,个数是未知的,所以定义变量。
            2,6的倍数咋表示?只要是对6能整除,也就是没有余数。
            3,需要对1-100之间所以的数都要进行判断是不是6的倍数。如果是,需要对个数变量进行自增。
            4,怎么完成1-100之间呢?使用循环。
    
    
            步骤:
            1,定义变量,记录个数。记录1-100变化的数。
            2,定义循环,遍历1-100.
            3,在循环中对1-100的数字进行判断。
            4,满足条件,个数变量自增。
            */
    
            //1,定义变量,记录个数。
            int count = 0;
            for (int x = 1; x<=100 ; x++ )
            {
                //对数值进行判断,是否是6的倍数。
                if(x % 6 == 0)
                    count++;
            }
    
            System.out.println("count="+count);
        }
    } 

8. HexTest

class HexTest 
    {
        public static void main(String[] args) 
        {
            /*
            //需求3:对给定的整数26,获取其十六进制的表现形式。★★★★
            思路;
            1,啥是十六进制,数据的一种表现形式,好处在于可以表现形式更短。
                原理:将二进制中4位为一个十六进制位。
            2,哦,原理就是对给定的整数进行二进制的每4位的获取。
            3,咋获取每4位呢?其实就是获取四位中的1,可以通过&位运算的方式完成。
            4,而且想要获取下四位,可以通过对原数据进行无符号右移的方式。
    
    
    
            步骤:
            1,定义变量记录该整数。
            2,对改变量进行&运算 既然是获取四位,就&四个1,二进制四个1就是十进制的15.
            3,对原数据进行无符号右移四位。
            */
            /*
            int num = 26;
            //&15.获取最低四位。
            int n1 = num & 15;
            System.out.println("n1="+(char)(n1-10+'a'));
            //对num进行右移。
            num = num >>> 4;
            //在进行&15.
            int n2 = num & 15;
            System.out.println("n2="+n2);
          //          97  98   99   100  101  102 
          //'0'-'9' 'a' 'b' 'c'  'd'  'e'   'f'
          //0 -  9  10  11  12   13   14    15    
            */
    
            /*
            以上的动作不合适。
            运算很重复。到底&15的结果是否需要做这个字母转换,无法确定。
            所以通过学习的语句来完成。
            1,通过循环,完成重复的运算。
            2,通过if完成是否需要转成字母的判断。
            */
    
            int num = 3567;//26=0x1A
    
            for(int x=0; x<8; x++)
            {
                int n = num & 15;
                if(n>9)
                    System.out.println((char)(n-10+'A'));
                else
                    System.out.println(n);
                num = num >>> 4;
            }
            //后记:两个问题未解决,1,反了。2,多零。烦!需要对多数据进行存储。期待中。。。
    
        }
    } 

9. IfDemo

class IfDemo
    {
        public static void main(String[] args) 
        {
            //1,if格式一。
            int x = 1;
            if(x > 1)
            {
                System.out.println("yes");
            }
            System.out.println("over");
    
            //2,if格式二:
            /*
            if(条件表达式)
            {
                执行语句;
            }
            else//否则。
            {
                执行语句;
            }
    
            */
    
            int a = 3;
            if(a>1)
            {
                System.out.println("yes");
            }
            else
            {
                System.out.println("no");
            }
    
            int b = 1,c;
            if(b>1)
            {
                c = 100;
            }
            else
            {
                c = 200;
            }
            //和三元运算符很像。  理解为三元运算符是if else 的简写格式。
            //区别?不是所有的if else都能简化的。为什么呢?因为三元运算符运算完必须有结果!
    
    
    
            /*
            格式三:
            if(条件表达式)
            {
                执行语句;
            }
            else if (条件表达式)
            {
                执行语句;
            }
            ……
            else
            {
                执行语句;
            }
    
            */
    
            int i = 3;
    
            if(i > 1){
                System.out.println("a");
            }
            else if(i > 2)
            {
                System.out.println("b");
            }
            else if(i > 3)
            {
                System.out.println("c");
            }
            else
            {
                System.out.println("d");
            }
        }
    } 

10. IfTest

class IfTest 
    {
        public static void main(String[] args) 
        {
            /*
            需求1:根据用户的给定的数值,显示该数值对应的星期。如:2,星期二。
            思路;
                1,咋获取这个数值呢?来源有很多,不确定,只要是不确定的数据。都用变量存储。
                    只要操作该变量即可。
                2,数值不确定,如何显示对应的星期呢?那就需要对该数值进行判断。
                3,咋显示呢?通过输出语句就可以显示在屏幕上。
    
    
            步骤:
            1,定义变量。记录数据。
            2,通过判断结构语句if对该变量进行判断。
            3,根据不同的条件,通过输出语句显示不同的结果。
            */
    
            //1,定义变量。记录数据。
            int week = 9;
    
            //2,通过判断结构语句if对该变量进行判断。
            if(week == 1)        
                //3,根据不同的条件,通过输出语句显示不同的结果。
                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+"没有对应的星期");
    
    
            /*
    
            需求2:根据用户的给定月份数据,显示该月份在哪个季节。
            3,4,5    春季
            6,7,8    夏季
            9,10,11    秋季
            12,1,2    冬季
            思路;
    
            步骤:
    
            填代码。
    
    
            */
    
        }
    } 

11. OperateDemo4

class OperateDemo4 
    {
        public static void main(String[] args) 
        {
    
    
            System.out.println(2*8);
        }
    } 

12. OperateDemo5

class OperateDemo5 
    {
        public static void main(String[] args) 
        {
    
            //三元运算符,格式:(条件表达式)?表达式1:表达式2;
    
            int x = 1,y;
    
            y = x>1?100:200;
    
            System.out.println("y="+y);
    
    
    
        }
    } 

13. OperateTest

class OperateTest 
    {
        public static void main(String[] args) 
        {
    
            //需求1,最有效率运算2乘以8.通过位移运算。
            System.out.println(2<<3);
    
            //需求2,对两个整数变量的值进行互换(不需要第三方变量)
            int a = 3, b = 7;
            /*
            a = b;a = 7;
            b = a;b = 7;
            错误。
            */
    
            /*
            //通过第三方变量的形式进行置换。
            int temp = a;
            a = b;
            b = temp;
            */
            /*
            //通过和的形式。有个弊端,两个数据较大,可能会超出int范围。
            a = a + b;//a = 3 + 7;
            b = a - b;//b = 3 + 7 - 7; b = 3;
            a = a - b;//a = 3 + 7 - 3;  a = 7;
            */
    
            //技巧。异或。
            a = a ^ b;// a = 3 ^ 7;
            b = a ^ b;// b = 3 ^ 7 ^ 7;
            a = a ^ b;// a = 3 ^ 7 ^ 3;
    
            System.out.println("a="+a+",b="+b);
    
    
            //需求3:对给定的整数26,获取其十六进制的表现实行。
    
    
            //需求4:三个数,想知道最大的是哪个?
    
            int x=4,y=8,z=2;
    
            System.out.println("max=");
    
    
        }
    } 

14. SwitchDemo

class SwitchDemo 
    {
        public static void main(String[] args) 
        {
    
            /*
            switch(表达式)
            {
                case 取值1:
                    执行语句;
                    break;
                case 取值2:
                    执行语句;
                    break;
                …...
                default:
                    执行语句;
                    break;
            }
    
            */
    
            int x = 5;
    
            /*
    
            switch语句特点:
            1,答案的书写没有顺序。
            2,匹配的答案被执行,一直执行到break结束或者执行到switch语句结束。
            */
            switch(x){//byte,short,int,char  5.0 enum  String
                case 5:
                    System.out.println("a");
                    //break;
                default:
                    System.out.println("c");
                    //break;    
                //1,定义答案:
    
                case 3:
                    System.out.println("b");
                    //break;
    
            }
    
            System.out.println("Hello World!");
        }
    } 

15. SwitchTest

class SwitchTest 
    {
        public static void main(String[] args) 
        {
            //需求1:根据用户的给定的数值,显示该数值对应的星期。如:2,星期二。
            int week = 9;
    
            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.out.println(week+"没有对应的星期");
                    break;
    
            }
            System.out.println("Hello World!");
    
            /*
            switch和if的区别?
    
            if可以用于判断数值,也可以判断区间,
            只要运算结果是boolean类型,都可以进行判断。
    
            switch用于对固定的几个值,进行判断。判断的值的类型有限。
    
    
            */
    
            /*
            需求:根据用户的给定月份数据,显示该月份在哪个季节。
            3,4,5    春季
            6,7,8    夏季
            9,10,11    秋季
            12,1,2    冬季
            用switch完成。
            */
    
        }
    } 

16. WhileDemo

class WhileDemo 
    {
        public static void main(String[] args) 
        {
    
            /*
            循环结构。循环语句什么时候用呢?
            当对某些语句需要执行很多次时,就使用循环结构。
            */
            int x = 1;
            while(x<3)//;
            {
                System.out.println("x="+x);
                ++x;
            }
    
            System.out.println("Hello World!");
        }
    } 

17. WhileTest

class WhileTest 
    {
        public static void main(String[] args) 
        {
            /*
            while语句练习:
    
            需求1:显示1-10.
    
            int x = 1;
            while(x<11)
            {
                System.out.println(x);
                x++;
            }
    
            */
    
    
            /*
    
            需求2:运算1-10的和。
            思路:
            1+2+3+4....
             3+3
              6+4
            每次重复的都是和+下一个数。
            和是确定的吗?不确定。需要变量。
            下一个数是确定的吗?不确定,需要变量。
            和+下一个数在重复,说明需要执行多次,用循环。
            下一个数,是有规律的。自增。
    
            步骤;
            1,定义变量。一个记录和,一个记录下一个数。
            2,需要循环结构。
            3,循环中需要进行和+下一个数的运算。并让下一个数自增。
    
    
            */
            //1,定义变量。一个记录和,一个记录下一个数。
            int i = 1;
            int sum = 0;
            //2,需要循环结构。
            while(i<=10)
            {
                //3,循环中需要进行和+下一个数的运算。并让下一个数自增。
                sum = sum + i;
                i++;
            }
            System.out.println("sum="+sum);
    
    
    
            /*
            需求;获取1-100之间,6的倍数的个数。
    
            */
    
        }
    } 

18. Test

class Test 
    {
        public static void main(String[] args) 
        {
    
    
            /*
    
            需求2:根据用户的给定月份数据,显示该月份在哪个季节。
            3,4,5    春季
            6,7,8    夏季
            9,10,11    秋季
            12,1,2    冬季
            思路;
            1,咋获取数据呢?关我什么事,我只要关注变量。
            2,对这个数据进行判断。
    
    
            步骤:
                1,定义变量。
                2,定义if语句判断。
    
            填代码。
            if(x==3 || x==4 || x==5)
                System.out.println(x+"是春天");
            else if(x==6 || x==7 || x==8)
                System.out.println(x+"是夏天");
            else if(x==9 || x==10 || x==11)
                System.out.println(x+"是秋天");
            else if(x==12 || x==1|| x==2)
                System.out.println(x+"是冬天");
            else 
                System.out.println(x+"没有对应的季节");
    
    
            if(x>12 || x<1)
                System.out.println(x+"没有对应的季节");
            else if(x>=3 && x<=5)
                System.out.println(x+"是春天");
            else if(x>=6 && x<=8)
                System.out.println(x+"是夏天");
            else if(x>=9 && x<=11)
                System.out.println(x+"是秋天");
            else
                System.out.println(x+"是冬天");
            */
    
    
            /*
    
            //需求3:对给定的整数26,获取其十六进制的表现形式。★★★★
            思路;
            1,啥是十六进制,数据的一种表现形式,好处在于可以表现形式更短。
                原理:将二进制中4位为一个十六进制位。
            2,哦,原理就是对给定的整数进行二进制的每4位的获取。
            3,咋获取每4位呢?其实就是获取四位中的1,可以通过&位运算的方式完成。
            4,而且想要获取下四位,可以通过对原数据进行无符号右移的方式。
    
    
    
            步骤:
            1,定义变量记录该整数。
            2,对改变量进行&运算 既然是获取四位,就&四个1,二进制四个1就是十进制的15.
            3,对原数据进行无符号右移四位。
            */
            int num = 26;
            //&15.获取最低四位。
            int n1 = num & 15;
            System.out.println("n1="+(char)(n1-10+'a'));
            //对num进行右移。
            num = num >>> 4;
            //在进行&15.
            int n2 = num & 15;
            System.out.println("n2="+n2);
          //          97  98   99   100  101  102 
          //'0'-'9' 'a' 'b' 'c'  'd'  'e'   'f'
          //0 -  9  10  11  12   13   14    15    
    
    
    
    
            /*
            //需求4:三个数,想知道最大的是哪个?
    
            int a = 3, b = 4, c = 5;
    
            int tempMax = a>b?a:b;
    
            int max = tempMax>c?tempMax:c;
    
            */
    
    
            /*
            需求:根据用户的给定月份数据,显示该月份在哪个季节。
            3,4,5    春季
            6,7,8    夏季
            9,10,11    秋季
            12,1,2    冬季
            用switch完成。
    
            switch(x)
            {
                case 3:
                case 4:
                case 5:
                    System.out.println(x+"是春天");
                    break;
                case 6:
                case 7:
                case 8:
                    System.out.println(x+"是夏天");
                    break;
                case 9:
                case 10:
                case 11:
                    System.out.println(x+"是秋天");
                    break;
                case 12:
                case 1:
                case 2:
                    System.out.println(x+"是冬天");
                    break;
                default:
                    System.out.println(x+"没有对应的季节");
                    break;
            }
    
            */
    
    
            /*
            需求:要求在屏幕上显示下列内容。★★★★★
            大圈套小圈的思想。for嵌套循环。
            54321
            5432
            543
            54
            5
    
            */
            System.out.println("------------------------");
            for ( int x=1; x<=5 ;x++ )
            {
                for (int y=5; y>=x  ; y--)
                {
                    System.out.print(y);
                }
                System.out.println();
            }
    
    
            /*
            //九九乘法表★★★★★
            1*1=1 
            1*2=2 2*2=4
            1*3=3 2*3=6 3*3=9
            */
    
            System.out.println("------------------------");
            for(int x=1;x<=9; x++)
            {
                for(int y=1; y<=x; y++)
                {
                    System.out.print(y+"*"+x+"="+y*x+"\t");//  \t :制表符  \n
                }
                System.out.println();
            }
    
    
    /*        
    第一题
    int x = 1,y=1;
    
    if(x++==2 & ++y==2)
    {
        x =7;
    }
    System.out.println("x="+x+",y="+y);x=2,y=2
    ---------------------------------------------------
    第二题
    int x = 1,y = 1;
    
    if(x++==2 && ++y==2)
    {
        x =7;
    }
    System.out.println("x="+x+",y="+y);x=2,y=1;
    ---------------------------------------------------
    第三题
    int x = 1,y = 1;
    if(x++==1 | ++y==1)
    {
        x =7;
    }
    System.out.println("x="+x+",y="+y);x=7,y=2
    ---------------------------------------------------
    第四题
    int x = 1,y = 1;
    if(x++==1 || ++y==1)
    {
        x =7;
    }
    System.out.println("x="+x+",y="+y);x=7,y=1;
    
    
    ---------------------------------------------------
    第五题
    boolean b = true;
    
    if(b==false)  //如果写成if(b=false)有结果吗?如果有,结果是?
        System.out.println("a");
    else if(b)
        System.out.println("b");
    else if(!b)
        System.out.println("c");
    else
        System.out.println("d");
    
    
    ---------------------------------------------------
    
    第六题
    int x = 2,y=3;
    
    switch(x)
    {
        default:
            y++;
        case 3:
            y++;
        case 4:
            y++;
    }
    
    System.out.println("y="+y);//6
    
    
            */
    
        }
    }
发布了21 篇原创文章 · 获赞 0 · 访问量 59

猜你喜欢

转载自blog.csdn.net/qq_42745404/article/details/105036053