Data type conversion, flow control, a random number, keyboard input

Data type conversion

Q: Can operation with different data types?
A: Yes

1.1 automatic type conversion (implicit)

Concept: calculating different data types, data type small range of data is automatically converted into a wide range of data types
byte-> short-> int-> long-> float-> double
format: Data types wide range variable name = small value data type;

1.2 casts (strong rpm)

Concepts: a specific format to convert a large value data range to a small range of data type
format: a small range of data type variable name = (target type) a wide range of data values;
Note:
1, the basic data type is not recommended mandatory converter
2, byte, short, char during operation, automatically converted to int

Process Control

Function Meaning: that is, the flow of control.
No matter how the current flow is controlled, as long as out of control, the program will still be executed sequentially. Flow control is to make it out of order, to achieve a certain purpose.

Sequence Structure

Consistent with the step of writing the code sequence runs a program

Branch structure

if、else

  • If a single structure
    if(条件){
        语句a
    }
    语句b
    逻辑:判断条件是否满足,如果满足则执行语句a,再执行语句b;
反之,直接执行语句b。
  • Standard if-else
    if(条件){
        语句a
    }
    else{
        语句b
    }
    语句c
    逻辑:条件满足,执行语句a;反之,执行语句b;语句c无论如何都会执行。  
注:if、else的是互斥的;if、else中必须有一个被执行

Note:
When the if () without braces it to find the first semicolon if following ;in the content, if it is empty before the semicolon is nothing execute. The same applies corresponds else.

    int i = 3;
    if (i < 2)
        System.out.println("S");
    System.out.println("B");
  • Multi-criteria if-else
    if(条件1){
        语句a
    }
    else if(条件2){
        语句b
    }
    else if(条件3){
        语句c
    }
    ...
    else{
        语句n
    }
    语句m
    逻辑:依次判断每一个条件,当有一个条件满足时,就执行对应代码段中的语句,然后执行语句m;
如果没有任何一个语句满足条件,则执行最后一个else的语句n,然后再执行语句m。
  • Nested conditional if-else
    format: code blocks or else if the if-else there continues Analyzing

    Select structure

    Keywords: switch, case, break, default

    switch(表达式){
    case 值1:
        语句a
        break;
    case 值2:
        语句b
        break;
    case 值3:
        语句c
        break;
    ...
    default:
        语句d
        break;
    }

Logic: The results of the calculation expression, this result with the comparison value after case, if the same, this case after the statement is executed;
the same result value of the expression is not a case, if the statement is executed after the default .

Note:

  1. Data type expression can be placed there byte, short, int, char, String, enumeration
  2. Result type after the value of the case must be an expression of the same type
  3. The value must be unique case
  4. default is not necessary, only when there is no need to have a case to meet and use the default result; and the location is arbitrary, usually on the last
  5. Effect break the end of the entire switch-case structure
  6. When the case is determined to satisfy the condition when he has performed, if there is to break out. Not to proceed until the break. When the case does not meet the conditions skipped this case the implementation of a case.

Without the use of penetrating break: break will lead to immediate interruption jump. As long as there is no interruption will continue execution, then the default on the front, or in the middle, case performed while back to default, will perform the following default down from the case.

In a case when a certain value of the result of the matching expression, BREAK not written, the program performs a downward order, the next encountered BREAK know, or know the end of the switch-case structure

这是计算月份某一年第几月的第几号是这一年的多少天。
倒着取能够把前面的月份都算上,以后有这样的算法,可以这样计算。
    int sum = 0;

    switch (month) {
        case 12:
            sum += 30;
        case 11:
            sum += 31;
        case 10:
            sum += 30;
        case 9:
            sum += 31;
        case 8:
            sum += 30;
        case 7:
            sum += 31;
        case 6:
            sum += 31;
        case 5:
            sum += 30;
        case 4:
            sum += 31;
        case 3:
            sum += 28;
        case 2:
            sum += 31;
        case 1:
            sum += day;
        

----------------------------------------------------------------
    所有满足条件的月份都可以向下执行
    int month = 5;
    if(month >= 1 && month <= 12){
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            System.out.println("大");
            break;
        case 2:
        case 4:
        case 6:
        case 9:
        case 11:
            System.out.println("小");
            break;
    }
------------------------------------------------------------------

先执行一边case发现没有满足条件的,之后执行default,然后发现没有break,又把所有的case又执行了一边。
    int score = 44;
    switch (score / 10) {
    default:
        System.out.println("E");
    case 10:
    case 9:
        System.out.println("A");
    case 8:
        System.out.println("B");
    case 7:
        System.out.println("C");
    case 6:
        System.out.println("D");

    }

    输出:
    E
    A
    B
    C
    D

Loop structure

  • In the next day4 notes

Random number, keyboard input

Keyboard input java.util.Scanner

Steps for usage:

  1. Objects created Scanner
    Scanner sc = new Scanner (System.in) ;
  2. The method determines whether the input received call
    String s = sc.next (); receiving the String to return
    int in = sc.nextInt (); int receiving data, returns an int

random number

Steps for usage:

  1. Create Random object
    Random r = new Random ();
  2. Method calls random integer
    r.nextInt (); int random number in the entire range of
    r.nextInt (int bound); random data is 0 ~ bound-1, including 0, also contains bound-1
    NOTE: random [m , n] equation nextInt (n-m + 1) + m
    Random random = new Random();
    double randomNum = random.nextInt();
    System.out.println(randomNum);

    想要随机一个一个23-44
    double randomNum = random.nextInt(22)+23;//这里面的22是要考虑到0的感受的
    System.out.println(randomNum);

    想要随机的数就是[m-n] 从m-n的随机数
    公式:(n-m+1)+m

additional

  • Objects and interfaces are reference data types, reference data transfer is the address type
  • All conditional, the first to do is to determine the legitimacy, only after judgment. This avoids useless condition determination

Guess you like

Origin www.cnblogs.com/macht/p/11567490.html