Learning objectives
1,Master keyboard entry Scanner Basic use of 2,Master three if Branch statement structure, execution process and use 3,master switch Branch statement structure, execution process and use 4,master Eclipse Instrumental debug Commissioning use
Chapter 1 data input
Data input is program acquisition, which refers to obtaining the data entered by the user's keyboard. How to implement it in java language?
We can get the user's input through the Scanner class.
1.1 use of scanner class
1.1.1 Guide Package
Use the import keyword to import the package, import the package before all the code of the class, and introduce the type to be used, Java All classes under Lang package do not need to be imported.
Scanner class in Java Util package, so you need to import this class.
Format:
import Package name.Class name;
1.1.2 creating objects
Format:
Data type variable name = new data type(parameter list);
1.1.3 calling method
Format:
Variable name.Method name();
Example:
Get the integer entered by the keyboard.
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { //create object Scanner sc= new Scanner(System.in); // Friendly tips System.out.println("Please enter an integer:"); //receive data int n = sc.nextInt(); //output data System.out.println("n= " + n); } }
1.2 Scanner exercise
Requirements:
Use the keyboard to enter the scores of 90 points, 94 points and 82 points of the three students, and then use the operator to compare them to obtain the highest score and print the results.
Example:
import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { // Create keyboard entry object Scanner Scanner sc = new Scanner(System.in); //Keyboard input three student scores and assign them to three variables respectively. System.out.println("Please enter the first student grade:"); int score1 = sc.nextInt(); System.out.println("Please enter the second student grade:"); int score2 = sc.nextInt(); System.out.println("Please enter the third student grade:"); int score3 = sc.nextInt(); // Use the ternary operator to obtain the higher score values of the first two students and save them with temporary variables. int tempScore = score1 > score2 ? score1 : score2; //Use the ternary operator to obtain the temporary score value and the higher score value of the third student, and save it with the highest score variable. int maxScore = tempScore > score3 ? tempScore : score3; // Output results System.out.println("The highest scores of the three students are:" + maxScore +"branch"); } }
Chapter 2 branch structure
In the process of a program execution, the execution order of each statement has a direct impact on the result of the program. Therefore, we must know the execution process of each statement. Moreover, many times we need to control the execution order of statements to realize the functions we want to complete.
In Java, process control statements can be divided into the following three categories:
-
Sequential structure
-
Branch structure (if, switch)
-
Loop structure (for, while, do... while)
Among them, the so-called sequential structure, that is, after the program enters the main() method entry, it is executed from top to bottom. As follows:
// main() method entry public static void main(String[] args){ //Execute sequentially, and run from top to bottom according to the writing order System.out.println(1); System.out.println(2); System.out.println(3); }
This sequential structure is very common, and we have used this structure in our previous exercises. However, in actual development, there may be some judgment or other factors that need to control the execution of statements, so branch structure and loop structure appear.
Next, let's explain the branch structure in detail.
2.1 if conditional branch statement
According to the actual business requirements, if conditional branch statements can be divided into the following three forms:
1,if Single branch statement 2,if...else... Double branch statement 3,if...else if ...else...Multi branch statement
The ternary operators we learned earlier can be converted using if statements.
2.1.1 if single branch statement
1. Introduction to if statement
- Syntax format:
if(Conditional expression){ Statement body; } //Other statements
- Pseudo code example:
if(Xiao Ming scored more than 700 points in the college entrance examination){ Xiao Ming can go to Harvard; } //Other statements Look at the results of others
2. if statement execution process
(1) First, the value of the conditional expression is calculated;
(2) If the value of the conditional expression is true, the statement body is executed;
(3) If the value of the conditional expression is false, the statement body will not be executed;
(4) Continue to execute other subsequent statements.
3. if statement exercise
public static void main(String[] args){ System.out.println("start"); // Define two variables int a = 10; int b = 20; //Variables are judged using if if (a == b){ System.out.println("a be equal to b"); } int c = 10; if(a == c){ System.out.println("a be equal to c"); } System.out.println("end"); }
2.1.2 if... else double branch statement
The above if single branch statement only cares about what happens if the condition is true. Sometimes, we care about what happens if it doesn't work. Therefore, the if... else double branch statement was born.
1. Introduction to if... else statement
- Syntax format:
if(Conditional expression) { Statement body 1; } else() { Statement body 2; } //Other statements
- Pseudo code example:
if(Xiao Ming scored more than 700 points in the college entrance examination){ Xiao Ming can go to Harvard; }else{ Xiao Ming chooses to reread it once; } //Other statements Look at the results of others
2. if... else statement execution process
(1) First, the value of the conditional expression is calculated;
(2) If the value of the conditional expression is true, execute statement body 1;
(3) If the value of the conditional expression is false, execute statement body 2;
(4) Continue to execute other subsequent statements.
3. if... else statement exercise
public static void main(String[] args) { System.out.println("start"); //Define two variables int a = 10; int b = 20; //Requirement: judge whether a is greater than b. If yes, the value of a output on the console is greater than b; otherwise, the value of a output on the console is not greater than b if(a > b) { System.out.println("a The value of is greater than b"); } else { System.out.println("a The value of is not greater than b"); } System.out.println("end"); }
4. if... else case
-
**Requirements: * * let the user give an integer arbitrarily. Please use the program to judge whether the integer is odd or even, and output whether the integer is odd or even on the console.
-
analysis:
① In order to give an arbitrary integer, enter a data with the keyboard;
② To judge whether an integer is even or odd, there are two cases to judge, using the if... else structure;
③ To judge whether even numbers need to use the remainder operator to realize this function, number% 2 = = 0;
④ According to the judgment, the corresponding content is output on the console. -
code implementation
public static void main(String[] args) { //In order to give an arbitrary integer, enter a data with the keyboard. (guide package, create object and receive data) Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer:"); int number = sc.nextInt(); //To judge whether an integer is even or odd, there are two cases to judge. Use if Else structure //To judge whether even numbers need to use the remainder operator to realize this function number% 2 = = 0 //According to the judgment, the corresponding content is output on the console if(number%2 == 0) { System.out.println(number + "It's an even number"); } else { System.out.println(number + "It's an odd number"); } }
2.1.3 if... else multi branch statements
If single branch condition is to focus on how one condition is established. If... else double branch statement is to focus on how one condition is established and how it fails. In other cases, our conditions may be diverse, and we should also pay attention to different execution in different cases. Therefore, we derive the multi branch statements of if... else and if... else.
1. Introduction to if... else statement
- Syntax format:
if (Conditional expression 1) { Statement body 1; } else if (Conditional expression 2) { Statement body 2; } ... }else if (Conditional expression n) { Statement body n; } else { Statement body n+1; } //Other statements
- Pseudo code example
if(Xiao Ming scored more than 700 points in the college entrance examination){ Xiao Ming can go to Harvard; }else if(Xiao Ming's college entrance examination score is more than 600, but not more than 700){ Xiao Ming chooses to go to Wuhan University; } else { // I didn't even get 600 points Xiao Ming really reread it; } //Other statements Look at the results of others
2. If... else if... else execution process
(1) First, the value of conditional expression 1 is calculated to determine whether the result is true or false;
(2) If true, execute statement body 1, and then execute other statements at the bottom;
(3) If it is false, continue to evaluate conditional expression 2 to judge whether the result is true or false;
(4) If true, execute statement body 2, and then execute other statements at the bottom;
(5) If it is false, continue to evaluate the conditional expression... To judge whether the result is true or false;
(6) ...
(7) If no conditional expression is true, the statement body n+1 in else is executed.
3. If... else if... else exercise
Enter a week (1, 2,... 7) on the keyboard and output the corresponding Monday, Tuesday,... Sunday
- Input 1 output Monday
Input 2 output Tuesday
Input 3 output Wednesday
Input 4 output Thursday
Input 5 output Friday
Input 6 output Saturday
Input 7 output Sunday - Input other digital output digital error.
public static void main(String[] args) { System.out.println("start"); // Requirement: enter a week (1,2,... 7) on the keyboard, and output the corresponding Monday, Tuesday Sunday Scanner sc = new Scanner(System.in); System.out.println("Please enter a number of weeks(1~7): "); int week = sc.nextInt(); if(week == 1) { System.out.println("Monday"); } else if(week == 2) { System.out.println("Tuesday"); } else if(week == 3) { System.out.println("Wednesday"); } else if(week == 4) { System.out.println("Thursday"); } else if(week == 5) { System.out.println("Friday"); } else if(week == 6) { System.out.println("Saturday"); } else if(week == 7) { System.out.println("Sunday"); } else { System.out.println("Wrong number!"); } System.out.println("end"); }
4. if... else case
-
Demand: Xiaoming is about to take the final exam. Xiaoming's father told him that he will give him different gifts according to his different test scores. If you can control Xiaoming's score, please use the program to realize what kind of gift Xiaoming should get and output it on the console.
-
Reward rules:
One 95 ~ 100 mountain bike
Play once in 90 ~ 94 amusement parks
One 80 ~ 89 transformers toy
Beat the fat under 80 -
analysis:
(1) Xiao Ming's test score is unknown. You can use the keyboard to obtain the value;
(2) Because there are many kinds of awards, they belong to a variety of judgments, which are implemented in the format of if... else if... else;
(3) Set corresponding conditions for each judgment;
(4) Set corresponding rewards for each judgment.
-
code implementation
public static void main(String[] args) { //Xiao Ming's test score is unknown. You can use the keyboard to get the value Scanner sc = new Scanner(System.in); System.out.println("Please enter a score:"); int score = sc.nextInt(); //Because there are many kinds of rewards, they belong to a variety of judgments. If else if... Else format implementation //Set the corresponding conditions for each judgment //Set corresponding rewards for each judgment //Data test: correct data, boundary data, error data if(score>100 || score<0) { System.out.println("The score you entered is incorrect"); } else if(score>=95 && score<=100) { System.out.println("One mountain bike"); } else if(score>=90 && score<=94) { System.out.println("Play in the playground once"); } else if(score>=80 && score<=89) { System.out.println("A transformer toy"); } else { System.out.println("Fat beat"); } }
2.2 switch select branch statement
In the previous section, we handled the weeks entered by the user and the student's grades using the multi conditional branch statement if... else if... else. In each condition judgment, you need to execute an expression statement to judge whether it is true. In addition, Java provides a more convenient multi conditional branch statement switch.
2.2.1 switch introduction
- switch statement format:
switch(expression) { case Constant value 1: Statement body 1; break; case Constant value 2: Statement body 2; break; ... default: Statement body n+1; break; }
- Pseudo code example
switch(1) { case 1: It's Monday; break; case 2: It's Tuesday; break; ... default: The number of weeks you entered is not valid!; break; }
2.2.2 switch execution process
- First, calculate the value of the expression;
- Then, compare the case from top to bottom. Once there is a corresponding value match, the corresponding statement will be executed, and the break will end in the process of execution.
- Finally, if all case s do not match the value of the expression, the body of the default statement is executed, and the program ends.
2.2.3 switch exercise
-
switch statement practice - spring, summer, autumn and winter
-
Demand: there are 12 months in a year, belonging to four seasons: spring, summer, autumn and winter. Enter a month on the keyboard. Please use the program to judge which season the month belongs to and output it.
-
Demonstration effect
Input: 1, 2, 12 output: Winter
Input: 3, 4, 5 Output: Spring
Input: 6, 7, 8 output: Summer
Input: 9, 10, 11 output: Autumn
Input: other digital output: digital error
-
-
code implementation
public static void main(String[] args) { //Define month variables and judge seasons int month = 6; //switch statement implementation selection switch(month) { case 1: System.out.println("winter"); break; case 2: System.out.println("winter"); break; case 3: System.out.println("spring"); break; case 4: System.out.println("spring"); break; case 5: System.out.println("spring"); break; case 6: System.out.println("summer"); break; case 7: System.out.println("summer"); break; case 8: System.out.println("summer"); break; case 9: System.out.println("autumn"); break; case 10: System.out.println("autumn"); break; case 11: System.out.println("autumn"); break; case 12: System.out.println("winter"); break; default: System.out.println("The month number you entered is incorrect!"); break; } }
**Note: * * in the switch statement, the data types of expressions can be byte, short, int, char, enum (enumeration class, JDK5 starts to support), String (String, JDK7 starts to support).
2.2.4 case penetration
In the switch statement, if break is not written after the case, penetration will occur, that is, it will not judge the value of the next case and run directly backward until a break is encountered or the overall switch ends.
public static void main(String[] args) { //Define month variables and judge seasons int month = 6; //switch statement implementation selection switch(month) { case 12: case 1: case 2: System.out.println("winter"); break; case 3: case 4: case 5: System.out.println("spring"); break; case 6: case 7: case 8: System.out.println("summer"); break; case 9: case 10: case 11: System.out.println("autumn"); break; default: System.out.println("The month number you entered is incorrect"); break; } }
Chapter 3 debug and debugging of Eclipse tools
In the previous code testing process, we directly use Run As in the main() method of the code startup class. The program in this way will be directly started and executed. However, sometimes the program execution may report errors. We want to know the execution process and sequence of the code, as well as the changes of relevant variables and data during the execution process. Therefore, we need to start the running project using the Debug mode.
Here, let's use the if... else if... else case code written before for demonstration.
3.1 introduction to eclipse tool Debug startup
Step 1: open the java code file, double-click the mouse on the left to set the breakpoint, or right-click the Toggle Breakpoint on the left
Step 2: click the "spider" button above eclipse to start debug debugging or right-click the main() method to run Debug As.
Or:
Step 3: display the debug frame. The debug mode interface is divided into five areas.
(1) The debug area displays what code is currently being debugged and how many lines it is in;
(2) The variables area displays the values of the variables.
(3) Console area.
3.2 eclipse tool Debug button implementation introduction
1.Indicates that the current implementation continues to run until the next breakpoint. The shortcut key is F8. 2.To interrupt the whole process is to end the program 3.Indicates entering the current method. The shortcut key is F5. 4.Indicates to run the next line of code, and the shortcut key is F6. 5.It means to exit the current method and return to the calling layer. The shortcut key is F7.