javaDay02.md

Operator (1-4 key points to master)

  1. Arithmetic operators: +,-, *, /,%, ++,-
  2. Assignment operator: =
  3. Relational operators:>, <,> =, <=, ==,! = Instanceof
  4. Logical operators: &&, || ,!
  5. Bitwise operators: &, |, ^, ~, >>, <<, >>> (understand !!!)
  6. Conditional operators:?,:
  7. Extended assignment operators: + =,-=, * =, / =
        //二元运算符
        //ctrl+D :复制当前行到下一行

        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
long a = 122222222222222222L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);  //long
        System.out.println(b+c+d);  //int
        System.out.println(c+d);  //int
//关系运算符返回的结果:正确,错误   布尔值

        int a = 10;
        int b = 20;
        int c = 22;

        System.out.println(c%b);  //取余   
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);

Self-incrementing and decrementing operator

//++  --   自增   自减
        int a = 3;
        int b = a++;  //执行完这行代码后,先给b赋值,再自增
        //a++  a = a + 1
        System.out.println(a);

        int c = ++a; //执行完这行代码后,先自增,后赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
//幂运算
        //使用工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);

Logical Operators

    //逻辑运算符
        //与(and)  或(or)  非(取反)

        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a && b));//逻辑与,两个变量都为真,结果才为true
        System.out.println("a || b :"+(a || b)); //逻辑或,两个变量有一个为真,则结果才为true
        System.out.println("! (a && b):"+!(a && b)); //逻辑非,如果为真,则变为假


        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);//逻辑与,如果c<4为假,就不会执行c++<4;
        System.out.println(d);
        System.out.println(c); //c的值为5

Bit operation

 /*
      * A = 0011 1100
      * B = 0000 1101
      *
      * A&B = 0000 1100  //全1为1,有0为0
      * A|B = 0011 1101  //全0为0,有1为1
      * A^B = 0011 0001  //相同为0,不同为1
      * ~B  = 1111 0010  //取反
      *
      *
      * */

      //2*8=16   2*2*2*2
      //效率极高!!!
      //<<  *2
      //>>  /2
      /*
       0000 0000    0
       0000 0001    1
       0000 0010    2
       0000 0011    3
       0000 0100    4
       0000 1000    8
       0001 0000    16
       */
        System.out.println(2<<3); //16

Ternary operator and summary

  int a = 10;
        int b = 20;

        a += b;  //a = a + b

        System.out.println(a);

        //字符串连接符   +  ,String
         
        System.out.println(""+a+b); //3020
        System.out.println(a+b+"");  //50
 //三元运算符
        //x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 80;
        String type = score < 60 ?"不及格":"及格"; //必须掌握
        //if
        System.out.println(type);

Packet mechanism

package pkg1[. pkg2[. pkg3……]];

Generally use the company domain name inversion as the package name:

com.baidu.www

In order to be able to use the members of a certain package, we need to explicitly import the package in the Java program. Use the "import" statement to complete this function

import package1[.package2……].(Classname|*);
package com.kuang.operator;

import com.kuang.operator.*; //导入全部  *为通配符

javadoc

The javadoc command is used to generate your own API documentation

Parameter information

  1. @author author name
  2. @version version number
  3. @since indicates that the earliest used jdk version is required
  4. @param parameter name
  5. @return return value
  6. @throws exception thrown
package com.kuang.operator;

/**
 * @author Hao
 * @version 1.0
 * @since 1.8
 */



public class Demo10 {
    String name;


    /**
     * @author Hao
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
    //通过命令行   javadoc 参数 java文件
    //面向百度编程
    
}

java flow control

  1. User interaction Scanner
  2. Sequential structure
  3. Select structure
  4. Cyclic structure
  5. break & continue

Scanner object

Get user input through Scannery class

Basic syntax:

Scanner s = new Scanner(System.in);
import java.util.Scanner;

public class Demo3 {
    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();
    }

}

The input string is obtained by the next () and nextLine () methods of the Scanner class. Before reading, we generally need to use hasNext () and hasNextLine () to determine whether there is input data.

next() hasNext()
  1. The input must be completed after valid characters are read
  2. The next () method will automatically remove the blanks encountered before entering valid characters.
  3. Only after entering a valid character can the space entered after it be used as a separator or terminator
  4. next () cannot get a string with spaces
import java.util.Scanner;

public class Demo1 {
    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();


    }
next() hasNextLine()
  1. With Enter as the end character, the NextLine () method returns all the characters before entering the carriage return.
  2. Get blank
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于存放键盘收据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收:");

        //判断用户有没有输入字符串
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            String str = scanner.nextLine();
            System.out.println("输入的内容为:"+str);
        }

        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }

Advanced use of Scanner

import java.util.Scanner;

public class Demo4 {
    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("输入的不是小数数据!");
        }

Exercise

import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数字:");
        //和
        double sum = 0;

        //计算输入了多少个数字
        int i = 0;
        //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
        while (scanner.hasNextDouble()) {
            double x = scanner.nextDouble();
            i = i + 1;  // i++
            sum = sum + x;
            System.out.println("你输入了第"+i+"个数据,当前结果sum="+sum);
        }
        System.out.println("这"+i+"个数字的和是:"+sum);
        System.out.println("这"+i+"个数字的平均值是:"+(sum/i));

        scanner.close();
    }

}

Sequential structure

  1. The basic structure of java is a sequential structure . Unless otherwise specified, it will be executed sentence by sentence in order.
  2. Sequential structure is the simplest algorithm structure
  3. It is a basic algorithm structure indispensable to any algorithm

Select structure

  1. if single selection structure
  2. if double selection structure
  3. if multiple choice structure
  4. Nested if structure
  5. switch multi-select structure

if single selection structure

grammar:

if(){
    //如果布尔表达式为true将执行的语句
}

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        // equals()判断字符串是否相等
        if (s.equals("Hello")){
            System.out.println(s);

        }
        System.out.println("End");
        scanner.close();
    }

if double selection structure

grammar:

if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        //考试分数大于60分就是及格,小于60分就不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score  = scanner.nextInt();

        if (score>60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }

        scanner.close();
    }
}

if multiple choice structure

grammar:

if(布尔表达式1){
    //如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
    //如果布尔表达式2的值为true执行代码
}else{
    //如果以上布尔表达式都不为true执行代码
}
import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        //考试分数大于60分就是及格,小于60分就不及格
        Scanner scanner = new Scanner(System.in);
        /*
        if 语句至多有1个else语句,else 语句在所有的 else if 语句之后。
        一旦其中一个 else if 语句检测为 true, 其他的 else if 以及 else 语句都将跳过执行。
         */
        
        
        System.out.println("请输入成绩:");
        int score  = scanner.nextInt();

        if (score==100){
            System.out.println("S级");
        }else if (score<100 && score>=90){
            System.out.println("A级");
        }else if (score<90 && score>=80){
            System.out.println("B级");
        }else if (score<80 && score>=70){
            System.out.println("C级");
        }else if (score<70 && score>=60){
            System.out.println("D级");
        }else if (score<60 && score>=0){
            System.out.println("不及格");
        }else{
            System.out.println("成绩不合法!");
        }


        scanner.close();

    }
}

Nested if structure

grammar:

if(布尔表达式 1){
    ////如果布尔表达式 1的值为true执行代码
    if(布尔表达式 2){
        ////如果布尔表达式 2的值为true执行代码
    }
}

switch multi-select structure

The variable types in the switch statement can be:
  1. byte、short、int 或 char
  2. switch supports the String type
  3. case label must be a string constant or a literal

grammar:

switch(expression){
    case value :
        //语句
        break;  //可选
    case value :
        //语句
        break;  //可选
    //你可以有任意数量的case语句    
    default :  //可选
        //语句
    
}
public class Demo4 {
    public static void main(String[] args) {
        // case穿透(无break)   //switch 匹配一个具体的值

        char grade = 'F';
        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;
            case 'E':
                System.out.println("不及格");
            default:
                System.out.println("未知等级");

        }
    }
}

Code 01

package sturct;

public class Demo5 {
    public static void main(String[] args) {
        String name = "小明";

        //字符的本质还是数字
        //反编译  java---class(字节码文件)-------反编译(IDEA)

        switch (name){
            case "小明":
                System.out.println("小明");
                break;
            case "孙悟空":
                System.out.println("孙悟空");
                break;
            default:
                System.out.println("弄啥嘞!");
        }
    }
}

Code 02 (decompiled code)

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package sturct;

public class Demo5 {
    public Demo5() {
    }

    public static void main(String[] args) {
        String name = "小明";
        byte var3 = -1;
        switch(name.hashCode()) {
        case 756703:
            if (name.equals("小明")) {
                var3 = 0;
            }
            break;
        case 23271124:
            if (name.equals("孙悟空")) {
                var3 = 1;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("小明");
            break;
        case 1:
            System.out.println("孙悟空");
            break;
        default:
            System.out.println("弄啥嘞!");
        }

    }
}

Cyclic structure

  1. while loop
  2. do while loop
  3. for loop
  4. Java5 introduces an enhanced for loop mainly used for arrays

while loop

grammar:

while(布尔表达式){
    //循环内容
}
  1. As long as the Boolean expression is true, the loop will continue to execute.
  2. Most of the time we will stop the loop, we need a way to invalidate the expression to end the loop.
  3. A small number of situations require the loop to be executed all the time, such as server request response monitoring.
  4. If the loop condition is always true, it will cause an infinite loop [dead loop]. In normal business programming, try to avoid an infinite loop. Will affect the performance of the program or cause the program to freeze and crash.
package sturct;

public class Demo6 {
    public static void main(String[] args) {
        //输出1~100
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
        }

    }
}
package sturct;

public class Demo7 {
    //死循环
    public static void main(String[] args) {
        while (true){
            //等待客户端连接
            //定时检查
            //。。。。。
        }
    }
}

Exercise:

package sturct;

public class Demo8 {
    public static void main(String[] args) {
        //计算1+2+3+……+100=?
        //高斯的故事
        int i = 0;
        int sum = 0;

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

Guess you like

Origin www.cnblogs.com/Mr-Sunday/p/12694306.html