Java jump statement -- writing idea and recursion of return and multiple loop structure

1, return statement

        1. The return statement jumps out of the current entire method, returns to the statement that calls the method, and continues to execute the program from the next statement of the statement.

        2. Two formats of return statement:

                2.1,return expression;

                        - Return a value to the statement calling the method;

                        - The data type of the return value must be consistent with the return value type in the method declaration or less precise than the declared data type

                2.2,return

                        - When the return type is declared null with void in the method declaration, this return type should be used and any value should be returned

2, Multiple cycles

        1. Three circulation modes: while;        \         do..while    ;      \         for;

        2. Multiple loops (loop nesting): a loop contains another complete loop structure. Any two loops can be nested with each other and can be cycled at any level, but generally no more than 3 levels.

        3. Multiple cycle execution process: the outer cycle variable changes once, and the inner cycle white energy changes once.

① Multiple loops use the continue statement;

/*
		3 There are 4 students in each class, and the students whose scores are greater than 85 are counted
*/

import java.util.Scanner;
public class Class{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Welcome to the math contest!");
		int sum = 0;
		for(int i = 1;i <= 3;i++){
			System.out.println("---------------");
			System.out.println("Please enter the class name:");
				int num = sc.nextInt();
			for(int j = 1;j <= 4;j++){
				System.out.println("Please enter page" + j + "Results of students:");
				int cj = sc.nextInt();
				if(cj > 85){
					System.out.println("Excellent results! Give praise!");
					sum++;
					continue;
				}else{
					System.out.println ("The result is OK! Keep working hard next time!");
					continue;
				}
			}
		}
		System.out.println("There are students with excellent results in each class of this competition" + sum + "A classmate!");
	}
}

② A hundred dollars for a hundred chickens;

/*
	One hundred chickens for one hundred dollars: one Rooster for 5 yuan, one hen for 3 yuan and three chicks for 1 yuan;
*/

public class MaiJi{
	public static void main(String[] args){
		for(int i = 0;i <= 20;i++){
			for(int j = 0;j <= 34;j++){
				for(int k = 0;k <= 300;k++){
					if(((i+j+k)==100)&&((5*i+3*j+k/3)==100)&&(k%3==0)){
						System.out.println("cock:" + i +"hen:" + j +"chick:" + k);
					}
				}
			}
		}
	}
}

③ Fibonacci sequence;

/*
		Fibonacci sequence
*/
import java.util.Scanner;
public class FeiBoNaQi{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the number of digits of the Fibonacci sequence you need:");
		int num = sc.nextInt();
		int x = 1;
		int y = 1;
		int z = 0;
		for(int i = 1;i <= num;i++){
			if(i == 1 || i == 2){
			System.out.print(1 + "\t");
			}else{
				z = x + y;
				x = y;
				y = z;
				System.out.print(z + "\t");
			}
		}
	}
}

④ 99 multiplication table;

/*
		99 multiplication table:
*/

public class Nine{
	public static void main(String[] args){
		for(int i = 1;i < 10;i++){
			for(int j = 1;j <= i;j++){
				System.out.print(j + "*" + i + "=" + (i*j) + "\t");
			}
			System.out.println();
		}
	}
}

⑤ Enter the membership information of three customers in a circular manner, and use the continue statement;

/*
	Cyclic entry of membership information of three customers
*/

import java.util.Scanner;
public class MyShopping{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Welcome MyShopping management system > Customer information management > Add customer information:");
		System.out.println();
		for(int i = 1;i <= 3;i++){
			System.out.println();
			System.out.print("Please enter your membership number<4 Bit integer>: ");
			int id = sc.nextInt();
			System.out.print("Please enter member birthday<month/Day, expressed as a two digit integer>: ");
			String bs = sc.next();
			System.out.print("Please enter member points:");
			int jf = sc.nextInt();
				if(id > 999){
					System.out.println("The member information you entered is:");
					System.out.println( id +"\t" + bs + "\t" + jf);
					continue;
				}else{
					System.out.println("Customer number" + id + "Is an invalid member number!");
					System.out.println("Failed to enter information!");
					continue;
			}
		}
	}
}

⑥ Multiple loop nested if statements;

/*
		3 There are 4 students in each class, and the students whose scores are greater than 85 are counted
*/

import java.util.Scanner;
public class Class{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Welcome to the math contest!");
		int sum = 0;
		for(int i = 1;i <= 3;i++){
			System.out.println("---------------");
			System.out.println("Please enter the class name:");
				int num = sc.nextInt();
			for(int j = 1;j <= 4;j++){
				System.out.println("Please enter page" + j + "Results of students:");
				int cj = sc.nextInt();
				if(cj > 85){
					System.out.println("Excellent results! Give praise!");
					sum++;
					continue;
				}else{
					System.out.println ("The result is OK! Keep working hard next time!");
					continue;
				}
			}
		}
		System.out.println("There are students with excellent results in each class of this competition" + sum + "A classmate!");
	}
}

⑦ Enter a number to judge whether it is odd or even;

import java.util.Scanner;
public class work1{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter a number:");
		int num = sc.nextInt();
		if(num % 2 == 0){
			System.out.println("even numbers");
		}else{
			System.out.println("Odd number");
		}
	}
}

⑧ The total score of the five students;

import java.util.Scanner;
public class work5{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the grade of 5 students in a class:");
		int sum = 0;
		for(int i = 1;i <= 5;i++){
			System.out.println("Please enter page"+ i +"Results of students:");
			int num = sc.nextInt();
			sum += num;
			continue;
		}System.out.println("5 The total score of students is:" + sum);
	}
}

3, Recursive algorithm:

        1. What is recursion: - the programming skill of program calling itself is called recursion;

                                   - A method in which a procedure or function directly or indirectly calls itself in its definition or description;

        2. Characteristics of recursive problems: - a large problem can be decomposed into several layers of simple subproblems;

                                          - The solution of the subproblem is consistent with that of its upper level problem;

                                          - The solution of the outer problem depends on the solution of the sub problem;

        3. Advantages and disadvantages of recursive algorithm:

                 3.1 advantages of recursion: simple program;

                3.2 disadvantages of recursion: - recursive calls will occupy a large number of system stacks, resulting in stack overflow;

                                           - When there are many levels of recursive calls, the speed is much slower than that of loops;

                3.3 use of recursion: - any problem that can be solved by recursion can also be solved by iteration;

                                                  - When the recursive method can reflect the problem more naturally, and it is easy to understand and debug

                                                  - And when efficiency is not emphasized, recursion can be used;

                                                  - When high performance is required, try to avoid using recursion, which takes time and memory;

Tags: Java Back-end

Posted on Wed, 01 Dec 2021 12:18:57 -0500 by Blade280891