逻辑运算

移位运算符

<< 左移1位相当于原数字2,左移N位相当于原数字2的N次方。(针对正数而言)

>>右移1位,相当于原数字/2,右移N位相当于原数字/2的N次方(针对正数而言)

>>>无符号右移,最右侧位不要了,最左侧补1

  • 计算机计算移位的效率高于计算加减乘除,当一个代码正好乘除2的N次方时,就可以用移位代替。
  • 移位负数或是移位位数太大都没意义

条件运算符

左右两边都必须是表达式

public static void main(String[] args) {
        int a =3;
        int b =5;
        int max = a>b ? a:b;//表达式a, b必须是同类型的,除非能发生隐式转换
        //产生的结果必须要被接收。
        //表达式不能单独存在,其产生结果必须要被使用 。
        System.out.println(max );//结果是5
    }

程序逻辑控制

f(布尔表达式){

//语句一

}else if{

//语句二

}else{

}

 public static void main(String[] args) {
        int num =2;
        if(num>=9){
            System.out.println("excllent");
        }else if(num>5 && num<9){
            System.out.println("nice");
        }else{
            System.out.println("not nice");
        }//输出结果是not nice
    }

判断闰年与否

public static void main(String[] args) {
        int year =2026;
        if(year%100==0){
            if(year%400==0){
                System.out.println(year+"是世纪闰年");
            }else{
                System.out.println(year + "不是世纪闰年");
            }
        }else{
            if(year%4==0){
                System.out.println(year + "是普通闰年");
            }else{
                System.out.println(year + "不是普通闰年");
            }
        }
    }//结果是2026不是普通闰年

switch case

public static void main(String[] args) {
        int a =3;
        switch(a){//a 不可以是double、float、boolean 、long
            case 1:
                System.out.println("nice");
                break;
                case 2:
                    System.out.println("good");
            case 3:
                System.out.println("great");
            case 4:
                System.out.println("excellent");
        }//输出结果是great
        //excellent
        //因为我没有加break,
    }

while

public static void main(String[] args) {
        int a=3;
        while(a<10){
            System.out.println(a);//运行结果是3、4、5、6、7、8、9
            a++;
        }//没有尽头的循环时没有意义的。
    }
计算0加到100的和
public static void main(String[] args) {
        int a=0;
        int sum=0;
        while(a <= 100){
            sum+=a;
            a++;
        }
        System.out.println("sum="+sum);//结果 5050
    }
{//计算阶乘
        int x =1;
        int sum = 0;
        while(x <=5){//while 后面没有分号
            int a= 1;
            int ret = 1;
            while(a<=x){
                ret *=a;
                a++;
            }
           sum += ret;
            x ++;
        }
        System.out.println(sum);//结果是153
    }

算能被2和3整除的数

{
        int a=1;
        while(a<100){
            if(a%6!=0)
            {
                a++;
                continue;
            }
            System.out.println(a);
            a++;
        }
    }

for循环

判断一个数是不是素数

{
      int x=13;
      int i=2;
      for(i=2;i<=x-1;i++)
      {
          if(x % i==0){
              System.out.println(x + "不是素数");
              break;
          }
      }
      if(i>x-1){
          System.out.println(x +"是素数");
      }
   }
posted @ 2026-02-10 12:42  甜面包蘸酷可乐  阅读(5)  评论(0)    收藏  举报