Java foundation 05 (program flow control)

Scanner: get different types of variables from the keyboard

  • Specific implementation steps
    • ① Guide Package
import java.util.Scanner;
  • ② Instantiation of Scanner:
Scanner scan = new Scanner(System.in);
  • ③ Call the relevant methods of the Scanner class to obtain the specified type
int age = scan.nextInt();
System.out.println(age);

  • Note: you need to enter a value of the specified type according to the specified method. If the input data type does not match the required data type, an exception will be reported: inputmismatch exception
    Cause the program to terminate.

example:

import java.util.Scanner;
class ScannerTest
{
	public static void main(String args[])
	{
		//2. Instantiation of scanner
		Scanner scan = new Scanner(System.in);//Input from keyboard
		
		//3. Call the relevant methods of Scanner class to obtain the specified type
		System.out.println("Please enter your name");
		String name = scan.next();	
		System.out.println(name);//output
		
		System.out.println("Please enter your gender (male)/(female):");
		String gender = scan.next();//"Male" - string
		char genderChar = gender.charAt(0);
		System.out.println(genderChar);
	}
}

1 sequential structure

2 branch structure

2.1 if... else structure

2.1.1 three formats of if... else structure

if(Conditional expression)
	{
		Execute expression 1
	}

Example 1:

int heartBeats = 79;
		if (heartBeats < 60 || heartBeats > 100)
		{
			System.out.println("Further inspection");
		}
if(Conditional expression)
	{
		Execute expression 1
	}
else
	{
		Execute expression 2
	}

Example 2:

int age1 = 19;
		if (age1 > 18)
		{
			System.out.println("You are an adult");
		}
		else
		{
			System.out.println("under age");
		}
if(Conditional expression)
	{
		Execute expression 1
	}
else if(Conditional expression)
	{
		Execute expression 2
	}
else if(Conditional expression)
	{
		Execute expression 3
	}
...
else
	{
		Execute expression n
	}

Example 3:

int age = 37;
		if (age < 0)
		{
			System.out.println("Illegal input");
		}
		else if (age < 18)
		{
			System.out.println("Adolescence");
		}
		else if (age < 35)
		{
			System.out.println("Young adults");
		}
		else
		{
			System.out.println("Other periods");
		}

2.1.2 description of if... else structure

  • ① When the if else structure is "one out of many", the last else is optional and can be omitted as needed
  • ② When multiple conditions are mutually exclusive, the order between conditional judgment statements and execution statements does not matter
  • ③ When more than one condition is an "include" relationship, it is usually necessary to place the declaration with a small scope on the declaration with a large scope. Otherwise, those with small scope will not have the opportunity to implement.
    example:
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter your grade");
		int num = scan.nextInt();
		if (num < 60)
		{
			System.out.println("No reward");
		}		
		else if (num <= 90)//Exchange with 80 below
		{
			System.out.println("A mobile phone");
		}
		else if (num <= 80)//
		{
			System.out.println("Flat");
		}
		else if (num == 100)
		{
			System.out.println("A car");
		}
  • ④ When there is an intersection relationship between multiple conditions, you need to consider which structure should be declared on it according to the actual situation
  • ⑤ If else structures can be nested. If there is only one statement in the if else structure, it can be omitted

2.2 switch case structure

2.2.1 format of switch case structure

switch((expression)
{
case Constant 1:
	Execute statement 1;
	//break;

case Constant 2:
	Execute statement 2;
	//break;

case Constant 3:
	Execute statement 3;
	//break;
...
default:
	Execute statement n;
	//break;
	
  }

2.2.2 description of switch case structure

  • ① Match the constants in each case in turn according to the values in the switch expression. Once the match is successful, the execution statement is invoked in the corresponding case structure. When the execution statement is completed, the execution statement in the other case structures will continue to be executed until the break keyword or the end of the switch-case structure ends.
  • ② break, which can be used in the switch case structure, means that once this keyword is executed, it will jump out of the switch case structure
  • ③ The expression in the switch structure can only be one of the following six data types: byte,short,int,char, enumeration type (new in JDK5.0), String type (new in JDK7.0)
  • ④ After a case, you can only declare a constant, not a range. Multiple cases in a switch case statement can be merged

example:

int score = scan.nextInt();
		int i = score / 10;
		switch (i)
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				System.out.println("fail,");
				break;
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				System.out.println("pass");
		}
  • ⑤ The break keyword is optional
  • ⑥ Defaultut: equivalent to else in an if else statement, and the default structure is optional

2.2.2 comparison of switch and if statements

  • ① If else structure and switch case structure can be interchanged under certain conditions
  • ② All that can use switch case structure can be converted to if else structure
  • ③ You can use either switch case structure (at the same time, there are not many switch case expressions) or if else structure. The switch case structure is preferred.
    Reason: the switch case structure is highly efficient

example:

int numDay = 0;
		System.out.println("Please enter the year:");
		int year = scan.nextInt();
		System.out.println("Enter month:");
		int month1 = scan.nextInt();
		System.out.println("Input date:");
		int day = scan.nextInt();
		switch (month1)
		{
		case 12:
			numDay += 30;
		case 11:
			numDay += 31;
		case 10:
			numDay += 30;
		case 9:
			numDay += 31;
		case 8:
			numDay += 31;
		case 7:
			numDay += 30;
		case 6:
			numDay += 31;
		case 5:
			numDay += 30;
		case 4:
			numDay += 31;
		case 3:
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			{
				numDay += 29;
			}
			else 
			{
				numDay += 28;
			}
			
		case 2:
			numDay += 31;
		case 1:
			numDay += day;

		}
		System.out.println( "Today is" + year + "year" + month1 + "month" + day + "day" + "For the second year of that year" + numDay + "day");
		

3 cycle structure

The four components of a loop statement

  • ① Initialization part
  • ② Loop condition part - boolean type expression
  • ③ Circulating body part
  • ④ Iterative part

explain:
Usually, the condition for the end of the loop is that the loop condition in ② returns false

3.1 while structure

To be added

3.2 do... while structure

To be added

3.3 for structure

  • Syntax format:
for (①Initialization part;②Cycle condition section ;④Iterative part )
		{
			③Circulating body part
		}
  • Execution process:
    ①—②—③—④—②—③—④—②—③—④—...—②

  • explain:

    • ② The loop condition part is a boolean type expression. When the value is false, exit the loop
    • ① The initialization part can declare multiple variables, but they must be of the same type, separated by commas
    • ④ There can be multiple variable updates, separated by commas

    Example 1: print the number and sum of all integers between 1 and 100 that are multiples of 7 (experience the idea of setting counters)

int i2,sum2 = 0, account = 0;
		for (i2 = 1;i2 <= 100 ;i2++ )
		{
			if (i2 % 7 ==0)
			{
				sum2 += i2;
				account ++;
			}
		}
		System.out.println("sum2= " + sum2 + ", account= " + account);

Example 2: write a program to cycle from 1 to 150, and print a value on each line. In addition, print "foo" on each multiple line of 3, print "biz" on each multiple line of 5, and print "baz" on each multiple line of 7.

The results are as follows:

int i ;
		for (i = 1;i <=150 ;i++ )
		{
			System.out.print(i);
			if (i % 3 == 0)
			{
				System.out.print(" foo");
			}

			if (i % 5 == 0)
			{
				System.out.print(" biz");
			}
			if (i % 7 == 0)
			{
				System.out.print(" baz");
			}
			System.out.println();
		}

Keywords: Java Spring jdk8

Added by nexgen_x on Fri, 19 Nov 2021 11:03:53 +0200