• java学习之路之javaSE基础2


    java学习之路之javaSE基础2

    所有的代码都是引用他人写的。

    1、逻辑运算符

     1 //&,|,^,!
     2         //int x = 10;
     3         //5 < x < 15
     4         //x > 5 & x < 15
     5         //逻辑与 & 并且and 遇false则false
     6         int a = 10;
     7         int b = 20;
     8         int c = 30;
     9         /*System.out.println(a < b & b < c);            //true & true = true
    10         System.out.println(a < b & b > c);                //true & false = false
    11         System.out.println(a > b & b < c);                //false & true = false
    12         System.out.println(a > b & b > c);                //false & false = false*/
    13 
    14         //逻辑或 或or 遇true则true
    15         /*System.out.println(a < b | b < c);                //true | true = true
    16         System.out.println(a < b | b > c);                //true | false = true
    17         System.out.println(a > b | b < c);                //false | true = true
    18         System.out.println(a > b | b > c);                //false | flase = false*/
    19 
    20         //逻辑异或 ^ 两边相同为false,两边不同为true
    21         /*System.out.println(a < b ^ b < c);                //true | true = false
    22         System.out.println(a < b ^ b > c);                //true | false = true
    23         System.out.println(a > b ^ b < c);                //false | true = true
    24         System.out.println(a > b ^ b > c);                //false | flase = false*/
    25 
    26         //逻辑非!
    27         System.out.println(!true);
    28         System.out.println(!!true);
    View Code
     1 /*
     2     &&与&的区别
     3     * a:最终结果一样。
     4     * b:&&具有短路效果。左边是false,右边不执行。
     5 
     6     ||与|的区别
     7     a:最终的结果是一样
     8     b:||具有短路效果,左边为true,右边不执行
     9     */
    10     public static void main(String[] args) {
    11         /*int a = 10;
    12         int b = 20;
    13         int c = 30;
    14         System.out.println(a < b && b < c);                //true && true = true
    15         System.out.println(a < b && b > c);                //true && false = false
    16         System.out.println(a > b && b < c);                //false && true = false
    17         System.out.println(a > b && b > c);                //false && false = false*/
    18 
    19         int x = 3;
    20         int y = 4;
    21         //System.out.println((++x == 3) & (++y == 4));    //false & false = false
    22         //System.out.println("x = " + x);                    //x = 4
    23         //System.out.println("y = " + y);                    //y = 5
    24         System.out.println("---------------------------");
    25         System.out.println((++x == 3) && (++y == 4));    //false & false = false
    26         System.out.println("x = " + x);                    //x = 4
    27         System.out.println("y = " + y);                    //y = 4
    28     }
    View Code

    2、位运算符

     1 class Demo1_Operator {
     2     public static void main(String[] args) {
     3         /*
     4         * &,|,^,~ 的用法
     5         * &:有0则0
     6         * |:有1则1
     7         * ^:相同则0,不同则1
     8         * ~:按位取反
     9         */
    10 
    11         System.out.println(6 & 3);                //2
    12         System.out.println(6 | 3);                //7
    13         System.out.println(6 ^ 3);                //5    
    14         System.out.println(~6);                    //-7?                    
    15     }
    16 }
    17 /*
    18     110
    19 &    011
    20 -----------
    21     010
    22 
    23     110
    24 |    011
    25 -----------
    26     111
    27 
    28     110
    29 ^    011
    30 -----------
    31     101
    32 
    33     00000000 00000000 00000000 00000110        6的原码反码补码都是本身
    34     11111111 11111111 11111111 11111001        对6取反
    35 -    00000000 00000000 00000000 00000001
    36 ---------------------------------------
    37     11111111 11111111 11111111 11111000        反码
    38     10000000 00000000 00000000 00000111        原码(-7)
    39 */
    View Code
     1 /*
     2         * 位异或运算符的特点
     3 
     4         * ^的特点:一个数据对另一个数据位异或两次,该数本身不变。
     5         */
     6 
     7         //System.out.println(5 ^ 10 ^ 10);
     8         //System.out.println(5 ^ 10 ^ 5);
     9 
    10         /*
    11         * 请自己实现两个整数变量的交换(不需要定义第三方变量)
    12         * 注意:以后讲课的过程中,我没有明确指定数据的类型,默认int类型。
    13         */
    14 
    15         int x = 10;
    16         int y = 5;
    17 
    18         //需要第三方变量,开发推荐用这种
    19         /*int temp;
    20         temp = x;
    21         x = y;
    22         y = temp;*/
    23 
    24         //不需要定义第三方变量,有弊端,有可能会超出int的取值范围
    25         /*x = x + y;                //10 + 5 = 15
    26         y = x - y;                //15 - 5 = 10
    27         x = x - y;                //15 - 10 = 5*/
    28 
    29         //不需要第三方变量,通过^来做
    30         x = x ^ y;                // 10 ^ 5 
    31         y = x ^ y;                // 10 ^ 5 ^ 5    y = 10
    32         x = x ^ y;                // 10 ^ 5 ^ 10  x = 5
    33 
    34         System.out.println("x = " + x + ",y = " + y);
    View Code
     1 /*
     2         *  <<:左移    左边最高位丢弃,右边补齐0
     3         *  >>:右移    最高位是0,左边补齐0;最高为是1,左边补齐1
     4         *  >>>:无符号右移 无论最高位是0还是1,左边补齐0
     5         *  最有效率的算出2 * 8的结果
     6         */
     7 
     8         //左移,向左移动几位就是乘以2的几次幂
     9         //System.out.println(12 << 1);        //24
    10         //System.out.println(12 << 2);        //48
    11 
    12         /*
    13         00000000 00000000 00000000 00001100        12的补码
    14      (0)0000000 00000000 00000000 000011000        24的补码
    15     (00)000000 00000000 00000000 0000110000        48的补码
    16         */
    17 
    18         //右移,向右移动几位就是除以2的几次幂
    19         //System.out.println(12 >> 1);
    20         //System.out.println(12 >> 2);
    21 
    22         /*
    23         00000000 00000000 00000000 00001100        12的补码
    24         000000000 00000000 00000000 0000110(0)    6
    25         0000000000 00000000 00000000 000011(00) 3
    26         */
    27 
    28         //最有效率的算出2 * 8的结果
    29         System.out.println(2 << 3);
    View Code

    3、三元运算符

    1 //(关系表达式) ? 表达式1 : 表达式2;
    2         int x = 10;
    3         int y = 5;
    4         int z;
    5         z = (x > y) ? x : y;
    6 
    7         System.out.println("z = " + z);
    View Code
     1 /*
     2 * a:导包
     3         * 格式:
     4             * import java.util.Scanner; 
     5         * 位置:
     6             * 在class上面。
     7     * b:创建键盘录入对象
     8         * 格式:
     9             * Scanner sc = new Scanner(System.in);
    10     * c:通过对象获取数据    
    11         * 格式:
    12             * int x = sc.nextInt();
    13 */
    14 import java.util.Scanner;
    15 class Demo2_Scanner {
    16     public static void main(String[] args) {
    17         /*Scanner sc = new Scanner(System.in);            //创建键盘录入对象
    18         System.out.println("请输入一个整数:");
    19         int x = sc.nextInt();                            //将键盘录入的数据存储在x中
    20         System.out.println(x);*/
    21 
    22         //录入两个整数
    23         Scanner sc = new Scanner(System.in);            //创建键盘录入对象
    24         System.out.println("请输入第一个整数:");
    25         int x = sc.nextInt();                            //将键盘录入的数据存储在x中
    26         System.out.println(x);
    27 
    28         System.out.println("请输入第二个整数:");
    29         int y = sc.nextInt();                            //将键盘录入的数据存储在y中
    30         System.out.println(y);
    31     }
    32 }
    View Code
     1 /*
     2         * A:案例演示
     3             * 比较两个整数是否相同
     4         * B:案例演示
     5             * 获取三个整数中的最大值
     6         */
     7 
     8         //比较两个整数是否相同
     9 
    10         /*int x = 10;
    11         int y = 10;
    12 
    13         //boolean b = (x == y) ? true : false;
    14         boolean b = (x == y);
    15         System.out.println("b = " +  b);*/
    16 
    17         //获取三个整数中的最大值
    18         int a = 10;
    19         int b = 20;
    20         int c = 30;
    21 
    22         //先比较任意两个数的值,找出这两个数中的最大值
    23         int temp = (a > b) ? a : b;
    24         //用前两个数的最大值与第三个数比较,获取最大值
    25         int max = (temp > c) ? temp : c;
    26         System.out.println("max =" + max);
    View Code
     1 /*
     2     * A:案例演示
     3         * 键盘录入练习:键盘录入两个数据,并对这两个数据求和,输出其结果
     4     * B:案例演示
     5         * 键盘录入练习:键盘录入两个数据,获取这两个数据中的最大值
     6 
     7     */
     8     public static void main(String[] args) {
     9         Scanner sc = new Scanner(System.in);    //创建键盘录入对象
    10         
    11         //键盘录入练习:键盘录入两个数据,并对这两个数据求和,输出其结果
    12         /*System.out.println("请输入第一个整数:");
    13         int x = sc.nextInt();                    //将键盘录入的数据存储在x中
    14         System.out.println("请输入第二个整数:");
    15         int y = sc.nextInt();                    //将键盘录入的数据存储在y中
    16         int sum = x + y;
    17         System.out.println(sum);*/
    18 
    19         //键盘录入练习:键盘录入两个数据,获取这两个数据中的最大值
    20         System.out.println("请输入第一个整数:");
    21         int x = sc.nextInt();                    //将键盘录入的数据存储在x中
    22         System.out.println("请输入第二个整数:");
    23         int y = sc.nextInt();                    //将键盘录入的数据存储在y中
    24 
    25         int max = (x > y) ? x : y;                //获取x和y中的最大值
    26         System.out.println("max = " + max);
    27     }
    View Code
     1 /*
     2 * A:案例演示
     3     * 键盘录入练习:键盘录入两个数据,比较这两个数据是否相等
     4 * B:案例演示
     5     * 键盘录入练习:键盘录入三个数据,获取这三个数据中的最大值
     6 */
     7 import java.util.Scanner;                            //导包
     8 class Test3_Scanner {
     9     public static void main(String[] args) {
    10         Scanner sc = new Scanner(System.in);        //创建键盘录入对象
    11 
    12         //键盘录入练习:键盘录入两个数据,比较这两个数据是否相等
    13         /*System.out.println("请输入第一个整数:");
    14         int x = sc.nextInt();                        //将键盘录入的数据存储在x中
    15         System.out.println("请输入第二个整数:");
    16         int y = sc.nextInt();                        //将键盘录入的数据存储在y中
    17 
    18         //boolean b = (x == y)? true : false;
    19         boolean b = (x == y);
    20         System.out.println(b);*/
    21 
    22         //键盘录入练习:键盘录入三个数据,获取这三个数据中的最大值
    23         System.out.println("请输入第一个整数:");
    24         int x = sc.nextInt();                        //将键盘录入的数据存储在x中
    25         System.out.println("请输入第二个整数:");
    26         int y = sc.nextInt();                        //将键盘录入的数据存储在y中
    27         System.out.println("请输入第三个整数:");
    28         int z = sc.nextInt();                        //将键盘录入的数据存储在y中
    29 
    30         //定义临时变量记录住比较出前两个变量中的最大值
    31         int temp = (x > y) ? x : y;
    32         //将比较后的结果与第三个变量中的值比较,比较出三个数中的最大值
    33         int max = (temp > z) ? temp : z;
    34         System.out.println(max);
    35     }
    36 }
    View Code

    4、顺序结构

    1                 System.out.println("Hello World!11111");
    2         System.out.println("Hello World!3333");
    3         System.out.println("Hello World!22222");
    4         System.out.println("Hello World!44444");            
    View Code

    5、选择结构if

     1 /*
     2 * C:if语句的格式1
     3 * 
     4         if(比较表达式) {
     5             语句体;
     6         }
     7 * D:执行流程:
     8     * 先计算比较表达式的值,看其返回值是true还是false。
     9     * 如果是true,就执行语句体;
    10     * 如果是false,就不执行语句体;
    11 */
    12 class Demo1_If {
    13     public static void main(String[] args) {
    14         int age = 17;
    15 
    16         if (age >= 18) {
    17             System.out.println("可以浏览本网站");
    18         }
    19 
    20         System.out.println("完了");
    21     }
    22 }
    View Code
     1 /*
     2 *    a:比较表达式无论简单还是复杂,结果必须是boolean类型
     3     * b:if语句控制的语句体如果是一条语句,大括号可以省略;
     4       * 如果是多条语句,就不能省略。建议永远不要省略。
     5     * c:一般来说:有左大括号就没有分号,有分号就没有左大括号
     6 
     7 */
     8 class Demo2_If {
     9     public static void main(String[] args) {
    10         int age = 17;
    11 
    12         if (age >= 18 && age <= 60) {
    13             System.out.println("可以浏览本网站");
    14             //int x = 10;                是两句话,int x声明是一句,x = 10 赋值是一句
    15         }
    16         System.out.println("完了");
    17         
    18     }
    19 }
    View Code
     1 /*
     2 * A:if语句的格式2
     3 * 
     4         if(比较表达式) {
     5             语句体1;
     6         }else {
     7             语句体2;
     8         }
     9 * B:执行流程:
    10     * 首先计算比较表达式的值,看其返回值是true还是false。
    11     * 如果是true,就执行语句体1;
    12     * 如果是false,就执行语句体2;
    13 * C:案例演示
    14     * a:获取两个数据中较大的值
    15     * b:判断一个数据是奇数还是偶数,并输出是奇数还是偶数
    16 
    17     * 注意事项:else后面是没有比较表达式的,只有if后面有。
    18 */
    19 class Demo3_If {
    20     public static void main(String[] args) {
    21         /*int x = 0;
    22         if (x == 1) {
    23             System.out.println("男厕所欢迎您");
    24         }else {
    25             System.out.println("女厕所欢迎您");
    26         }*/
    27         
    28         //a:获取两个数据中较大的值
    29         /*int x = 10;
    30         int y = 20;
    31         int z;
    32 
    33         if (x > y) {
    34             z = x;
    35         }else {
    36             z = y;
    37         }
    38 
    39         System.out.println(z);*/
    40         
    41         //b:判断一个数据是奇数还是偶数,并输出是奇数还是偶数
    42         int num = 11;
    43         if (num % 2 == 0) {
    44             System.out.println(num + "是一个偶数");
    45         }else {
    46             System.out.println(num + "是一个奇数");
    47         }
    48     }
    49 }
    View Code
     1 /*
     2 * A:案例演示
     3     * if语句和三元运算符完成同一个效果
     4 * B:案例演示
     5     * if语句和三元运算符的区别
     6     
     7     * 三元运算符实现的,都可以采用if语句实现。反之不成立。
     8     
     9     * 什么时候if语句实现不能用三元改进呢?
    10         * 当if语句控制的操作是一个输出语句的时候就不能。
    11         * 为什么呢?因为三元运算符是一个运算符,运算符操作完毕就应该有一个结果,而不是一个输出。
    12 
    13 */
    14 class Demo4_If {
    15     public static void main(String[] args) {
    16         int x = 10;
    17         int y = 20;
    18         int z;
    19 
    20         if (x > y) {
    21             //z = x;
    22             System.out.println(x + "是最大值");
    23         }else {
    24             //z = y;
    25             System.out.println(y + "是最大值");
    26         }
    27 
    28         //System.out.println(z);
    29 
    30         int a = 20;
    31         int b = 30;
    32 
    33         int c = (a > b)? a : b;
    34     }
    35 }
    View Code
     1 /*
     2 * A:if语句的格式3:
     3 * 
     4         if(比较表达式1) {
     5             语句体1;
     6         }else if(比较表达式2) {
     7             语句体2;
     8         }else if(比较表达式3) {
     9             语句体3;
    10         }
    11         ...
    12         else {
    13             语句体n+1;
    14         }
    15 * B:执行流程:
    16     * 首先计算比较表达式1看其返回值是true还是false,
    17     * 如果是true,就执行语句体1,if语句结束。
    18     * 如果是false,接着计算比较表达式2看其返回值是true还是false,
    19     
    20     * 如果是true,就执行语句体2,if语句结束。
    21     * 如果是false,接着计算比较表达式3看其返回值是true还是false,
    22     
    23     * 如果都是false,就执行语句体n+1。
    24 * C:注意事项:最后一个else可以省略,但是建议不要省略,可以对范围外的错误值提示 
    25 */
    26 class Demo5_If {
    27     public static void main(String[] args) {
    28         int x = 2;
    29         if (x == 1) {
    30             System.out.println("男厕所欢迎您");
    31         }else if (x == 0) {
    32             System.out.println("女厕所欢迎您");
    33         }else {
    34             System.out.println("无法识别您的性别");
    35         }
    36     }
    37 }
    View Code
     1 /*
     2 * A:案例演示
     3     * 需求:获取三个数据中的最大值
     4     * if语句的嵌套使用。
     5 */
     6 class Demo6_IfIf {
     7     public static void main(String[] args) {
     8         int a = 40;
     9         int b = 50;
    10         int c = 30;
    11         
    12         if (a > b) {
    13             if (a > c) {
    14                 System.out.println(a + "是最大值");
    15             }else {
    16                 System.out.println(c + "是最大值");
    17             }
    18 
    19         }else {    //b >= a
    20             if (b > c) {
    21                 System.out.println(b + "是最大值");
    22             }else {
    23                 System.out.println(c + "是最大值");
    24             }
    25         }
    26     }
    27 }
    View Code
     1 import java.util.Scanner;
     2 class Test1_If {
     3     public static void main(String[] args) {
     4         /*
     5         * A:练习1
     6         * 
     7                 需求:键盘录入一个成绩,判断并输出成绩的等级。
     8                 90-100 优
     9                 80-89  良
    10                 70-79  中
    11                 60-69  及
    12                 0-59   差
    13                 
    14         * B:练习2
    15             * 需求:
    16                 * 键盘录入x的值,计算出y的并输出。
    17                 
    18                 * x>=3    y = 2 * x + 1;
    19                 * -1<x<3    y = 2 * x;
    20                 * x<=-1    y = 2 * x - 1;
    21         */
    22         Scanner sc = new Scanner(System.in);
    23 
    24         //需求:键盘录入一个成绩,判断并输出成绩的等级。
    25         /*System.out.println("请输入学生成绩范围在1到100之间");
    26         int x = sc.nextInt();
    27 
    28         if (x >= 90 && x <= 100) {
    29             System.out.println("优");
    30         }else if (x >= 80 && x <= 89 ) {
    31             System.out.println("良");
    32         }else if (x >= 70 && x <= 79 ) {
    33             System.out.println("中");
    34         }else if (x >= 60 && x <= 69 ) {
    35             System.out.println("及");
    36         }else if (x >= 0 && x <= 59 ) {
    37             System.out.println("差");
    38         }else {
    39             System.out.println("成绩录入错误");
    40         }*/
    41 
    42         //需求: 键盘录入x的值,计算出y的并输出
    43         System.out.println("请输入一个整数:");
    44         int x = sc.nextInt();
    45         int y = 0;
    46         if (x >= 3) {
    47             y = 2 * x + 1;
    48         }else if (x > -1 && x < 3) {
    49             y = 2 * x;
    50         }else if (x <= -1) {
    51             y = 2 * x - 1;
    52         }
    53 
    54         System.out.println(y);
    55     }
    56 }
    View Code

    6、选择结构switch

     1 class Demo1_Switch {
     2     public static void main(String[] args) {
     3         /*
     4         * A:switch语句的格式
     5         *        int x = 10;
     6                 switch(表达式) {        //基本数据类型可以接收byte,short,char,int
     7                       case 值1:        //引用数据类型可以接收枚举(JDK1.5)String字符串(JDK1.7)
     8                         语句体1;
     9                         break;
    10                         case 值2:
    11                         语句体2;
    12                         break;
    13 14                         default:    
    15                         语句体n+1;
    16                         break;
    17                 }
    18          
    19         * B:switch语句的格式解释
    20         * C:面试题
    21             * byte可以作为switch的表达式吗?
    22             * long可以作为switch的表达式吗?
    23             * String可以作为switch的表达式吗?
    24         * C:执行流程
    25             * 先计算表达式的值
    26             * 然后和case后面的匹配,如果有就执行对应的语句,否则执行default控制的语句
    27         */
    28 
    29         String name = "rose";
    30         String gender = "妖";
    31         switch (gender) {
    32         case "男士":
    33             System.out.println(name + "是一位" + gender + "喜欢吃饭睡觉打dota");
    34         break;
    35         case "女士":
    36             System.out.println(name + "是一位" + gender + "喜欢逛街购物美容");
    37         break;
    38         default:
    39             System.out.println(name + "是一位" + gender + "打雌性激素维持美貌容颜");
    40         break;
    41         }
    42     }
    43 }
    View Code
     1 class Test1_Switch {
     2     public static void main(String[] args) {
     3         //* A:整数(给定一个值,输出对应星期几)
     4         int week = 1;
     5         switch (week) {
     6         case 1:
     7             System.out.println("星期一");
     8         break;
     9         case 2:
    10             System.out.println("星期二");
    11         break;
    12         case 3:
    13             System.out.println("星期三");
    14         break;
    15         case 4:
    16             System.out.println("星期四");
    17         break;
    18         case 5:
    19             System.out.println("星期五");
    20         break;
    21         case 6:
    22             System.out.println("星期六");
    23         break;
    24         case 7:
    25             System.out.println("星期日");
    26         break;
    27         default:
    28             System.out.println("对不起没有对应的星期");
    29         break;
    30         }
    31     }
    32 }
    View Code
     1 class Test2_Switch {
     2     public static void main(String[] args) {
     3         // A:看程序写结果:
     4 
     5         /*int x = 2;
     6         int y = 3;
     7         switch(x){
     8             default:
     9                 y++;
    10                 break;
    11             case 3:
    12                 y++;
    13             case 4:
    14                 y++;
    15         }
    16         System.out.println("y="+y);*/
    17     
    18     //B:看程序写结果:
    19 
    20         int x = 2;
    21         int y = 3;
    22         switch(x){
    23             default:
    24                 y++;
    25             case 3:
    26                 y++;
    27             case 4:
    28                 y++;
    29         }
    30         System.out.println("y="+y);
    31     }
    32 }
    View Code
     1 import java.util.Scanner;
     2 class Test3_SwitchIf {
     3     public static void main(String[] args) {
     4         /*
     5 
     6         * 键盘录入月份,输出对应的季节
     7         一年有四季
     8         3,4,5春季
     9         6,7,8夏季
    10         9,10,11秋季
    11         12,1,2冬季
    12         */
    13         Scanner sc = new Scanner(System.in);    //创建键盘录入对象
    14         System.out.println("请输入月份");
    15         int month = sc.nextInt();                //将键盘录入的结果存储在month
    16         /*switch (month) {
    17         case 3:
    18         case 4:
    19         case 5:
    20             System.out.println(month + "月是春季");
    21         break;
    22         case 6:
    23         case 7:
    24         case 8:
    25             System.out.println(month + "月是夏季");
    26         break;
    27         case 9:
    28         case 10:
    29         case 11:
    30             System.out.println(month + "月是秋季");
    31         break;
    32         case 12:
    33         case 1:
    34         case 2:
    35             System.out.println(month + "月是冬季");
    36         break;
    37         default:
    38             System.out.println("对不起没有对应的季节");
    39         break;
    40         }*/
    41 
    42         //用if语句来完成月份对应季节
    43         if (month > 12 || month < 1) {
    44             System.out.println("对不起没有对应的季节");
    45         }else if (month >= 3 && month <= 5) {
    46             System.out.println(month + "月是春季");
    47         }else if (month >= 6 && month <= 8) {
    48             System.out.println(month + "月是夏季");
    49         }else if (month >= 9 && month <= 11) {
    50             System.out.println(month + "月是秋季");
    51         }else {
    52             System.out.println(month + "月是冬季");
    53         }
    54     }
    55 }
    View Code
  • 相关阅读:
    Amazon Route 53
    监控应用程序负载均衡器ALB
    DynamoDB 流
    DynamoDB 中的限制
    Amazon SNS 消息属性
    关于基于 SAML 2.0 的联合身份验证
    Amazon EBS 性能提示
    Laravel5.1注册中间件的三种场景
    编译LNMP环境
    傻瓜式搭建LAMP环境
  • 原文地址:https://www.cnblogs.com/wanghongyun/p/9348884.html
Copyright © 2020-2023  润新知