Java Learning Notes 3-Process Control

Posted by spikypunker on Tue, 17 Mar 2020 03:23:02 +0100

Article Directory

Input & Output

input

Getting input from the console requires importing the java.util.Scanner class in Java to get different types of input by reading the corresponding type.

package note3;

/**
 * Created with IntelliJ IDEA.
 * Version : 1.0
 * Author : cunyu
 * Email : cunyu1024@foxmail.com
 * Website : https://cunyu1943.github.io
 * Date : 2019/12/18 17:30
 * Project : JavaLeaning
 * Package : note3
 * Class : InputAndOutput
 * Desc : Java Note 3
 */

import java.util.Scanner;

public class InputAndOutput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //        Enter the entire line and get the string
        System.out.println("Input the name");
        String strName = input.nextLine();

        //        Enter the whole line and get the integer
        System.out.println("Input the id");
        int intId = input.nextInt();

        System.out.println("Name is : " + strName);
        System.out.println("Id is : " + intId);
    }
}

output

  • Normal Output

    System.out.print ln is output and wrap without line break.

  • Format Output

    With placeholders, the array type is "formatted" as a specified string. Common placeholders are shown in the following table. Note that% represents a placeholder, and two consecutive%% are required to output%:

    placeholder Explain
    %d Format Output Integer
    %x Format Output Hexadecimal Integer
    %f Format Output Floating Point
    %e Formatted output floating point number represented by scientific counting
    %s format string

if judgment

  • Basic Grammar
if (condition)
{
	// do something if condition is true...
	...
}
if(condition)
{
	// do something if condition is true...
	...
} else
{
	// do something if condition is false...
}
  • Judgment of Equality of Variable Content between Reference Type and Reference Type

    • ==: used to determine whether the reference types are equal, used to determine whether two objects point to the same object;
    • equals(): Used to determine if the contents of variables of reference type are equal;
    public class Main()
    {
        public static void main(String[] args)
        {
        	String s1 = "hello";
            String s2 = "hello";
            if(s1 == s2)
            {
                System.out.println("s1 == s2");
            }
            
            // s1 will error NullPointerException if null
            if(s1.equals(s2))
            {
                System.out.println("sq equals s2");
            }
        }
    }
    

switch multiple selection

switch (option) {
case 1:
    ...
    break;
case 2:
    ...
    break;
case 3:
    ...
    break;
default:
    break;
}

option data type can be integer, string or enumeration type, PS: don't forget break and default;

While & do while loop

  • while: that is, let the computer do circular calculations according to the conditions, continue the cycle when the conditions are met, and exit the cycle when the conditions are not met.Before each cycle, first judge whether the condition is valid, then execute the statement inside the loop, otherwise jump out of the cycle directly;

    while(condition)
    {
        // Loop statement
    }
    // Continue with subsequent code execution
    
  • do...while: Execute the loop first, and then judge the condition, then continue the cycle if the condition is satisfied, exit the cycle when it is not satisfied, at least once;

    do{
        // Execute Loop Statement
    } while(condition);
    

for loop

  • Loop is achieved by using counters. First the counters are initialized, then the loop conditions are detected before each loop, and then the counters are updated after each loop.

    For (initial condition; loop detection condition; update counter after loop)
    {
    	//Loop execution statement    
    }
    
  • for loops can be missing initialization statements, loop conditions, and update statements per loop;

    // No End Conditions
    for(int i = 0; ; i++){
    	...
    }
    
    // No closing conditions and update statements
    for(int i = 0; ;){
        ...
    }
    
    // None of them are set
    for( ; ; ){
        ...
    }
    
  • for each loop: A variable used to iterate through all "iterative" data types that is not a counter but corresponds to each element in the array, but at the same time it cannot specify the order of iteration or obtain the array index;

    // For and for each loop array
    int[] array = {1,3,5,7,9};
    // for
    for(int i = 0; i < array.length; i++){
        System.out.println(array[i] + "\t");
    }
    // for each
    for(int n: array){
        System.out.println(n + "\t");
    }
    

    **PS:**The counter variable is defined inside the for loop, and the counter is not modified inside the loop;

break and continue

  • break:

    The cycle is used to jump out of the current cycle, often with if, always jumping out of the layer in which it is located.

    public class Main{
        public static void main(String[] args){
            int sum = 0;
            for(int i = 0; ; i++){
                sum += i;
                if(i == 100){
                    break;
                }
            }
            System.out.println(sum);
        }
    }
    
  • continue

    End the cycle ahead of time, proceed directly to the next cycle, often with if, extract to end the current cycle when conditions are met;

    public class Main{
        public static void main(String[] args){
            int sum = 0;
            for(int i = 0; i <= 100; i++){
                sum += i;
                // Calculate the sum of 1 + 2 + 3 +... + 100, but subtract 10 from it
                if(i == 10){
                    continue;
                }
            }
            System.out.println(sum);
        }
    }
    

summary

This paper summarizes the knowledge of input and output, if, switch, single and multiple loops, and jump and stop loops in process control.

95 original articles published. 68% praised. 130,000 visits+
Private letter follow

Topics: Java IntelliJ IDEA github