Day02

运算符

++ –

public class Demo01 {
    public static void main(String[] args) {
        // ++  --是自增,自减运算符
        int a=10;
        int b=a++;//先执行这段代码后,在完成自增
       //a=a+1
        int c=++a;//先自增,在执行这段代码    先自增  ++a=11+1=12  把12赋值给c
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);


        //幂运算  2^3  2*2*2=8   很多运算我们会使用一些工具类来操作
        double pow = Math.pow(2, 3);
        System.out.println(pow);
        
    }
}

逻辑运算符

&& (与) ||(或)!(非)

public class Demo02 {
    public static void main(String[] args) {
        boolean a= true;
        boolean b=false;
        System.out.println("a&&b:"+ (a&&b));//逻辑与运算:两个变量为真,则结果为真
        System.out.println("a||b:"+ (a||b));//逻辑或运算:两个变量有一个为真,则结果为真
        System.out.println("!(a&&b):"+ !(a&&b));//逻辑非运算:如果为真,则为假,如果为假则为真

    //短路运算  如果执行到false 就不往下运算了,直接舒服结果
        
        int c=5;
        boolean d=(c<5)&&(c++<4);
        
        System.out.println(c);
        System.out.println(d);


    }

}

位运算符

public class Demo03 {
    public static void main(String[] args) {
        /*
        * A = 0011 1100
        * B = 0000 1101
        * ----------------------------
        * A&B = 0000 1100   两个为1 则为1其他为0
        * A|B = 0011 1101   两个有一个为1则为1,否则 为0
        * A^B = 0011 0001   这两个相同为0 不同为1
        *  ~B = 1111 0010   取反
        *
        * 2*8怎么运算最快
        *
        * 2*2*2*2    
        * <<左移 *2      >>右移  /2   效率高   */
        System.out.println(2<<3);
    }
}

a+ a-

public class Demo04 {
    public static void main(String[] args) {
        int a =10;
        int b =20;
        a+=b;//a=a+b

        b+=a;//b=b+a
        System.out.println(a);
        System.out.println(b);

        //字符串连接符   +,String
        System.out.println(a+b);
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}

条件运算符

public class Demo05 {
    public static void main(String[] args) {
        //x?y:z
        //如果x==true,则结果为y,否则结果为z
        int score=30;
        String type = score<60?"不及格":"及格";
        System.out.println(type);

    }
}

包机制

一般利用公司域名倒置作为域名 com.baidu.www

package pkg1.pkg2;

导入包

import package1.package2.classname;

通配符 .*

import com.lv.base.*;//导入这个包下所有的类

JavaDoc

参数信息

@author 作者名

@version 版本号

@since 指明需要最早使用的jdk版本

@param 参数名

@return 返回值情况

@throws 异常抛出情况

Scanner对象

获取用户输入

java.util.Scanner;

通过scanner获取用户输入

Scanner s= new Scanner(System.in);

通过Scanner类的next()与nextLine()方法获取的字符串,在读取前我们一般需要使用 hasNext() 与hasNextLine()判断是否还有输入的数据。

package com.lv.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接受");
        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            String str = scanner.next();
            System.out.println("输入的内容为"+str);
        }
        //凡是属于IO流 (输入输出流)的类,如果不关闭就会一直占用资源,
        scanner.close();
    }
}

package com.lv.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接受");
        if (scanner.hasNext()){
            String str =scanner.nextLine();
            System.out.println("输入的内容为"+str);
        }
        scanner.close();
    }
}

package com.lv.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容分");
        String str =scanner.nextLine();
        System.out.println("输出的内容为"+str);
        scanner.close();
    }
}
package com.lv.scanner;


import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i=0;
        float f=0.0f;
        System.out.println("请输入整数");
        if (scanner.hasNextInt()){
            i= scanner.nextInt();
            System.out.println("输出的整数为"+i);
        }else {
            System.out.println("你输入的数不是整数");
        }
        System.out.println("请输入小数");
        if (scanner.hasNextFloat()){
            f=scanner.nextFloat();
            System.out.println("输出的小数为"+f);
        }else {
            System.out.println("你输入的数不是小数");
        }

        scanner.close();
    }

}

练习

package com.lv.scanner;

import java.util.Scanner;
//我们可以通过输入多个数字,并求其总和与平均值
public class Demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //计算有都少个数字
        int m=0;
        //总和
        double sum=0;
        System.out.println("请输入数字");
        //循环语句,判断是否还有输入,并对每一次的循环计算总和和输入的数字的个数
        while (scanner.hasNextDouble()){
            double x=scanner.nextDouble();
            m=m+1;
            sum=sum+x;
            System.out.println("你输入了第"+m+"个数,"+m+"个数的总和为"+sum);
        }
        System.out.println(m+"个数的总和为"+sum);
        System.out.println(m+"个数的平均为"+sum/m);
        scanner.close();

    }
}

循环语句

if语句至多有一个else语句,else语句在所有else if语句之后,if语句可以有若干个else if语句,它们必须在else语句之前

一旦其中一个else if 语句检测为true,其他的额else if语句将会条过

package com.lv.struct;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println();

//        System.out.println("请输入你的分数");
//        int score=scanner.nextInt();
//        if (score>60){
//                System.out.println("你的成绩达到60分以上及格");
//        }else {
//            System.out.println("你的成绩没达到60分不上及格");
//
//        }

        //-----------------------------------------------------------------------------------

//        System.out.println("请输入是否成功");
//        String str=scanner.nextLine();
//        if (str.equals("是")){
//            System.out.println("你成功开发出了软件 得到100万元");
//        }else{
//            System.out.println("你没有成功开发出软件,你需要自己出钱找人开发软件");
//        }

        System.out.println("请输入你的分数");
        int score=scanner.nextInt();
        if (score>=0 && score<=100) {   //设置输入分数区间
            if (score > 90) {
                System.out.println("你的成绩达到90分以上 成绩优秀");
            } else if (score > 60) {
                System.out.println("你的成绩达到60分以上 成绩及格");
            } else if (score < 60) {
                System.out.println("你的成绩未达到60分以上 成绩不及格");
            }
        }else
            System.out.println("你输入的分数不合法");

            scanner.close();

    }
}



switch

package com.lv.struct;



public class SwitchDemo01 {
    public static void main(String[] args) {
      //case 穿透  //  switch   匹配一个具体的值
        System.out.println("请输出你的等级A,B,C,D,");
        char grade ='F';
        System.out.println(grade);
        switch(grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("还需努力");
                break;
            default:
                System.out.println("未查询到成绩");
               

        }
    }
}

While Do While 循环语句

package com.lv.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        //-----------------------------------------------------------
//        while (i<100){                  //while 语句先判断在执行
//            i=i+1;//或者i++;
//            System.out.println(i);
//            sum=sum+i;
//
//        }
//        System.out.println("和"+sum);
        //------------------------------------------------------------
        do {                            //do while 语句先执行在判断,  保证循环体至少执行一次
            sum=sum+i;
            i++;

        }while(i<=100);
        System.out.println(sum);
    }

}

For 循环语句

package com.lv.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a=1;//初始化条件
        while (a<=100){//条件判断
            System.out.println(a);//循环体
            a+=2;//a=a+2   迭代
        }
        System.out.println("结束循环");
        for (a=1;a<=100;a+=2){
            System.out.println(a);
        }
        System.out.println("for循环结束");
    }
}

for 循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构

package com.lv.struct;

public class Demo03 {
    //用while或for循环输出1-1000之间能被5整除的数,且每行输出3个数
    public static void main(String[] args) {

        for (int i = 1; i <=1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");


            }
            if (i%15==0){
                System.out.println();
            }

        }
    }
}
package com.lv.struct;

public class Demo02 {
    public static void main(String[] args) {
        int oddSum=0;
        int evenSum=0;
        for (int i=0;i<=100;i++){
            if (i%2!=0){
                oddSum+=i;
            }else{
                evenSum+=i;
            }
        }
        System.out.println("奇数和为"+oddSum);
        System.out.println("偶数和为"+evenSum);
    }
}

99乘法口诀表

 package com.lv.struct;

public class Demo04 {
    public static void main(String[] args) {
        //1.先打印第一列,
        //2.把固定值1再用一个循环套起来
        //去掉重复项ij<i
        //调整样式
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <=i; j++) {
                System.out.print(i+"*"+j+"="+i*j+" ");
            }
            System.out.println();

        }
    }
}

三角形联练习

package com.lv.struct;

public class TestDemo {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {

            for (int j = 5; j >=i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 2; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();


        }
    }
}

方法

package com.lv.method;

public class Demo01 {
    //main 方法
    public static void main(String[] args) {
        int add = add(2, 3);
        System.out.println(add);

    }

    //加法
    //形式参数,用来定义
    public static int add(int a,int b){
        return a+b;
    }
}
package com.lv.method;

public class Demo02 {
    public static void main(String[] args) {
        int max = max(10, 10);
        System.out.println(max);
    }
    //比大小
    public static int max(int a,int b){
       int c=0;
       if (a==b){
           System.out.println("a=b");
           return 0;//终止方法
       }
        if(a>b){
            c=a;
        }else {
            c=b;
        }
        return c;
    }

}

方法重载

package com.lv.method;

public class Demo01 {
    //main 方法
    public static void main(String[] args) {
        int add = add(2, 3,4);
        System.out.println(add);

    }

    //加法

    public static int add(int a,int b){
        return a+b;
    }
    //方法重载,可以改类型,添加参数,但是方法名必须相同
    public static int add(int a,int b,int c){
        return a+b+c;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值