2021-07-29 review java basic day06 debug & basic exercise

1.Debug mode

1.1 what is Debug mode [understand]

It is a program debugging tool for programmers. It can be used to view the execution process of the program or to track the execution process of the program to debug the program.

1.2 operation process of debug mode [application]

  • How to add breakpoints
    • Select the code line to set the breakpoint, and click the left mouse button behind the line number area

  • How to run a program with breakpoints
    • Right click Debug in the code area to execute

  • Look where
    • Look at the Debugger window

  • Look at the Console window

  • Where
    • Click the arrow Step Into (F7), or press F7 directly

  • How to delete a breakpoint
    • Select the breakpoint to delete and click the left mouse button

  • If there are multiple breakpoints, you can click each one again. You can also delete all at once

2. Basic exercises

2.1 weight loss plan if version [application]

2.1.1 case requirements

Enter the number of weeks to display today's weight loss activities Monday: running Tuesday: swimming Wednesday: jogging Thursday: spinning Friday: boxing Saturday: mountain climbing Sunday: have a good meal

2.1.2 code implementation

/*
Idea:
1:Enter the number of a week on the keyboard and receive it with a variable
2:Judge the number of weeks, which is realized by if statement
3:Output the corresponding weight loss activity in the corresponding statement control
*/
public class Test01 {
	public static void main(String[] args) {
		//Enter the number of a week on the keyboard and receive it with a variable
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter a number of weeks:");
		int week = sc.nextInt();
		//Judge the number of weeks, which is realized by if statement
		if (week < 1 || week > 7) {
			System.out.println("The number of weeks you entered is incorrect");
		} else if (week == 1) {
			System.out.println("run");
		} else if (week == 2) {
			System.out.println("Swimming");
		} else if (week == 3) {
			System.out.println("Walk slowly");
		} else if (week == 4) {
			System.out.println("Spinning bike");
		} else if (week == 5) {
			System.out.println("Boxing");
		} else if (week == 6) {
			System.out.println("Mountain climbing");
		} else {
			System.out.println("Have a good meal");
		}
	}
}

2.2 weight loss plan switch version [application]

2.2.1 case requirements

Enter the number of weeks to display today's weight loss activities Monday: running Tuesday: swimming Wednesday: jogging Thursday: spinning Friday: boxing Saturday: mountain climbing Sunday: have a good meal

2.2.2 code implementation

/*
Idea:
1:Enter the number of a week on the keyboard and receive it with a variable
2:Judge the number of weeks, which is realized by switch statement
3:Output the corresponding weight loss activity in the corresponding statement control
 Guide Package:
1:Manual package import Java util. Scanner;
2:Shortcut key guide package Alt+Enter
3:Automatic bag guide
*/
public class Test02 {
	public static void main(String[] args) {
		//Enter the number of a week on the keyboard and receive it with a variable
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter a number of weeks:");
		int week = sc.nextInt();
		//Judge the number of weeks, which is realized by switch statement
		switch (week) {
		case 1:
			System.out.println("run");
			break;
		case 2:
			System.out.println("Swimming");
			break;
		case 3:
			System.out.println("Walk slowly");
			break;
		case 4:
			System.out.println("Spinning bike");
			break;
		case 5:
			System.out.println("Boxing");
			break;
		case 6:
			System.out.println("Mountain climbing");
			break;
		case 7:
			System.out.println("Have a good meal");
			break;
		default:
			System.out.println("Your week number is incorrect");
		}
	}
}

2.3 skip every seven days [ application ]

2.3.1 case requirements

When friends get together, they may play a game: every seven. The rule is: start counting from any number. When the number you want to report contains 7 or a multiple of 7, you should say: too. In order to help you better play the game, here we print out the data between 1-100 on the console that meets the rule of passing every seven. In this way, when you play games in the future, you will know what data to say: too.

2.3.2 code implementation

/*
Idea:
1:The data is between 1-100, and the for loop is used to obtain the data
2:According to the rules, use the if statement to judge the data: either the one bit is 7, or the ten bit is 7, or it can be divided by 7
3:Output the data that meets the rules in the console
*/
public class Test03 {
	public static void main(String[] args) {
		//The data is between 1-100, and the for loop is used to obtain the data
		for(int x=1; x<=100; x++) {
			//According to the rules, use the if statement to judge the data: either the one bit is 7, or the ten bit is 7, or it can be divided by 7
			if(x%10==7 || x/10%10==7 || x%7==0) {
				//Output the data that meets the rules in the console
				System.out.println(x);
			}
		}
	}
}

2.4 immortal rabbit [application]

2.4.1 case requirements

A pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what are the logarithm of rabbits in the twentieth month?

2.4.2 code implementation

/*
Idea:
1:In order to store the rabbit logarithm of multiple months, an array is defined, and the initialization of array elements is completed with dynamic initialization, with a length of 20
2:Because the logarithms of rabbits in the first and second months are known, which are all 1, the values of the first and second elements of the array are also 1
3:Calculate the rabbit logarithm of each month by loop
4:The value of the last element in the output array is the logarithm of the rabbit in the 20th month
*/
public class Test04 {
	public static void main(String[] args) {
		//In order to store the rabbit logarithm of multiple months, an array is defined, and the initialization of array elements is completed with dynamic initialization, with a length of 20
		int[] arr = new int[20];
		//Because the logarithms of rabbits in the first and second months are known, which are all 1, the values of the first and second elements of the array are also 1
		arr[0] = 1;
		arr[1] = 1;
		//Calculate the rabbit logarithm of each month by loop
		for(int x=2; x<arr.length; x++) {
			arr[x] = arr[x-2] + arr[x-1];
		}
		//The value of the last element in the output array is the logarithm of the rabbit in the 20th month
		System.out.println("The logarithm of rabbits in the 20th month is:" + arr[19]);
	}
}

2.5 Baiqian white chicken [application]

2.5.1 case requirements

Zhang Qiujian, an ancient mathematician in China, put forward the mathematical problems in the book Suanjing: a chicken Weng is worth five, a chicken mother is worth three, and a chicken chick is worth three. If you buy a hundred chickens for a hundred dollars, ask the chicken owner, the chicken mother and the chicken chick?

2.5.2 code implementation

/*
Idea:
1:Layer 1 loop is used to represent the range of chicken Weng. The variable of initialization expression is defined as x=0, and the judgment condition is x < = 20
2:The second layer loop is used to represent the range of chicken mother. The variable of initialization expression is defined as y=0, and the judgment condition is y < = 33
3:At this time, the variable used to represent the chick z = 100 – x – y
4:Judge whether the expression z%3==0 and the expression 5*x + 3*y + z/3 = 100 are valid at the same time. If so, output the corresponding
x,y,z The value of is the corresponding value of chicken Weng, chicken mother and chicken chick
*/
public class Test05 {
	public static void main(String[] args) {
		//Layer 1 loop is used to represent the range of chicken Weng. The variable of initialization expression is defined as x=0, and the judgment condition is x < = 20
		for(int x=0; x<=20; x++) {
			//The second layer loop is used to represent the range of chicken mother. The variable of initialization expression is defined as y=0, and the judgment condition is y < = 33
			for(int y=0; y<=33; y++) {
				//At this time, the variable used to represent the chick z = 100 – x – y
				int z = 100 - x - y;
				//Judge whether the expression z%3==0 and the expression 5*x + 3*y + z/3 = 100 are valid at the same time
				if(z%3==0 && 5*x+3*y+z/3==100) {
					System.out.println(x+","+y+","+z);
				}
			}
		}
	}
}

2.6 summation of array elements [application]

2.6.1 case requirements

There is such an array. The elements are {68,27,95,88171996,51210}. Find the sum of elements that meet the requirements in the array. The requirements are: the single bit and ten bit of the summation element cannot be 7, and can only be an even number

2.6.2 code implementation

/*
Idea:
1:Define an array and complete the initialization of array elements with static initialization
2:Define a summation variable with an initial value of 0
3:Traverse the array and get each element in the array
4:Judge whether the element meets the conditions. If the conditions are met, it will be accumulated
5:Outputs the value of the summation variable
*/
public class Test06 {
	public static void main(String[] args) {
		//Define an array and complete the initialization of array elements with static initialization
		int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
		//Define a summation variable with an initial value of 0
		int sum = 0;
		//Traverse the array and get each element in the array
		for(int x=0; x<arr.length; x++) {
			//Judge whether the element meets the conditions. If the conditions are met, it will be accumulated
			if(arr[x]%10!=7 && arr[x]/10%10!=7 && arr[x]%2==0) {
				sum += arr[x];
			}
		}
		//Outputs the value of the summation variable
		System.out.println("sum:" + sum);
	}
}

2.7 judge whether the two arrays are the same [ application ]

2.7.1 case requirements

Define a method to compare whether the contents of two arrays are the same

2.7.2 code implementation

/*
Idea:
1:Define two arrays and use static initialization to initialize the array elements respectively
2:Define a method to compare whether the contents of two arrays are the same
3:Compare whether the contents of the two arrays are the same. Just follow the steps below
 First, compare the length of the array. If the length is different, the contents of the array must be different, and return false
 Secondly, traverse, compare each element in the two arrays, and return false as long as there are different elements
 After traversal, the loop returns true
4:Call method and receive with variable
5:Output results
*/
public class Test07 {
	public static void main(String[] args) {
	//Define two arrays and use static initialization to initialize the array elements respectively
	int[] arr = {11, 22, 33, 44, 55};
	//int[] arr2 = {11, 22, 33, 44, 55};
	int[] arr2 = {11, 22, 33, 44, 5};
	//Call method and receive with variable
	boolean flag = compare(arr,arr2);
	//Output results
	System.out.println(flag);
	}
	
	//Define a method to compare whether the contents of two arrays are the same
	/*
	Two clear:
	Return value type: boolean
	Parameters: int[] arr, int[] arr2
	*/
	public static boolean compare(int[] arr, int[] arr2) {
		//First, compare the length of the array. If the length is different, the contents of the array must be different, and return false
		if(arr.length != arr2.length) {
			return false;
		}
		//Secondly, traverse, compare each element in the two arrays, and return false as long as there are different elements
		for(int x=0; x<arr.length; x++) {
			if(arr[x] != arr2[x]) {
				return false;
			}
		}
		//Finally, after the loop traversal is completed, it returns true
		return true;
	}
}

2.8 find the index position of elements in the array [application]

2.8.1 case requirements

An array arr = {19, 28, 37, 46, 50} is known; Enter a data on the keyboard to find the index of the data in the array.
And output the index value found in the console. If not found, output - 1

2.8.2 code implementation

/*
Idea:
1:Define an array and complete the initialization of array elements with static initialization
2:Enter the data to be searched with the keyboard and receive it with a variable
3:Define an index variable with an initial value of - 1
4:Traverse the array and get each element in the array
5:Compare the data entered on the keyboard with each element in the array. If the values are the same, assign the index corresponding to the value to the index variable, and
 End cycle
6:Output index variable
*/
public class Test08 {
	public static void main(String[] args) {
		//Define an array and complete the initialization of array elements with static initialization
		int[] arr = {19, 28, 37, 46, 50};
		//Enter the data to be searched with the keyboard and receive it with a variable
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the data to find:");
		int number = sc.nextInt();
		//Call method
		int index = getIndex(arr, number);
		//Output index variable
		System.out.println("index: " + index);
	}
	
	//Finds the index of the specified data in the array
	/*
	Two clear:
	Return value type: int
	Parameters: int[] arr, int number
	*/
	public static int getIndex(int[] arr, int number) {
		//Define an index variable with an initial value of - 1
		int index = -1;
		//Traverse the array and get each element in the array
		for(int x=0; x<arr.length; x++) {
			//Compare the data entered on the keyboard with each element in the array. If the values are the same, assign the index corresponding to the value to the index variable and end the cycle
			if(arr[x] == number) {
				index = x;
				break;
			}
		}
		//Return index
		return index;
	}
}

2.9 array element inversion [ application ]

2.9.1 case requirements

An array arr = {19, 28, 37, 46, 50} is known; Program to exchange the element values in the array. The exchanged array arr = {50, 46, 37, 28,19}; And output the exchanged array elements on the console.

2.9.2 code implementation

/*
Idea:
1:Define an array and complete the initialization of array elements with static initialization
2:Loop through the array. This time, the initialization statement defines two index variables. The judgment condition is that the start index is less than or equal to the end index
3:Variable exchange
4:Traversal array
*/
public class Test09 {
	public static void main(String[] args) {
		//Define an array and complete the initialization of array elements with static initialization
		int[] arr = {19, 28, 37, 46, 50};
		//Call the inverted method
		reverse(arr);
		//Traversal array
		printArray(arr);
	}
	
	/*
	Two clear:
	Return value type: void
	Parameter: int[] arr
	*/
	public static void reverse(int[] arr) {
		//Loop through the array. This time, the initialization statement defines two index variables. The judgment condition is that the start index is less than or equal to the end index
		for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
			//Variable exchange
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
		}
	}
	
	/*
	Two clear:
	Return value type: void
	Parameter: int[] arr
	*/
	public static void printArray(int[] arr) {
		System.out.print("[");
		for (int x = 0; x < arr.length; x++) {
			if (x == arr.length - 1) {
				System.out.print(arr[x]);
			} else {
				System.out.print(arr[x] + ", ");
			}
		}
		System.out.println("]");
	}
}

2.10 scoring by judges [ application ]

2.10.1 case requirements

In the programming competition, six judges score the contestants with an integer score of 0-100. The final score of the contestant is the average value of the four judges after removing the highest score and the lowest score (regardless of the decimal part).

2.10.2 code implementation

/*
Idea:
1:Define an array, complete the initialization of array elements with dynamic initialization, and the length is 6
2:Keyboard entry of judges' scores
3:Since there are 6 judges scoring, the operation of receiving judges' scores is improved by circulation
4:Define the method implementation to obtain the highest score (the maximum value of the array) in the array and call the method
5:Define the method implementation to obtain the lowest score (array minimum value) in the array and call the method
6:Define the method implementation to obtain the sum of all elements in the array (summation of array elements) and call the method
7:Calculate according to the calculation rules to get the average score
8:Output average score
*/
public class Test10 {
	public static void main(String[] args) {
		//Define an array, complete the initialization of array elements with dynamic initialization, and the length is 6
		int[] arr = new int[6];
		//Keyboard entry of judges' scores
		Scanner sc = new Scanner(System.in);
		//Since there are 6 judges scoring, the operation of receiving judges' scores is improved by circulation
		for(int x=0; x<arr.length; x++) {
			System.out.println("Please enter page" + (x + 1) + "Scoring of judges:");
			arr[x] = sc.nextInt();
		}
		//printArray(arr);
		//Define the method implementation to obtain the highest score (the maximum value of the array) in the array and call the method
		int max = getMax(arr);
		//Define the method implementation to obtain the lowest score (array minimum value) in the array and call the method
		int min = getMin(arr);
		//Define the method implementation to obtain the sum of all elements in the array (summation of array elements) and call the method
		int sum = getSum(arr);
		//Calculate according to the calculation rules to get the average score
		int avg = (sum - max - min) / (arr.length - 2);
		//Output average score
		System.out.println("The final score of the contestant is:" + avg);
	}
	
	/*
	Two clear:
	Return value type: int
	Parameter: int[] arr
	*/
	public static int getSum(int[] arr) {
		int sum = 0;
		for(int x=0; x<arr.length; x++) {
			sum += arr[x];
		}
		return sum;
	}
	
	/*
	Two clear:
	Return value type: int
	Parameter: int[] arr
	*/
	public static int getMin(int[] arr) {
		int min = arr[0];
		for(int x=1; x<arr.length; x++) {
			if(arr[x] < min) {
				min = arr[x];
			}
		}
		return min;
	}
	
	/*
	Two clear:
	Return value type: int
	Parameter: int[] arr
	*/
	public static int getMax(int[] arr) {
		int max = arr[0];
		for(int x=1; x<arr.length; x++) {
			if(arr[x] > max) {
				max = arr[x];
			}
		}
		return max;
	}
	
	//Traversal array
	public static void printArray(int[] arr) {
		System.out.print("[");
		for (int x = 0; x < arr.length; x++) {
			if (x == arr.length - 1) {
				System.out.print(arr[x]);
			} else {
				System.out.print(arr[x] + ", ");
			}
		}
		System.out.println("]");
	}
}

Keywords: Java

Added by deniscyriac on Tue, 04 Jan 2022 06:44:30 +0200