Java Experiment Report 3: Branch Programming

Java Experiment Report 3: Branch Programming

1. The purpose of the experiment

1. Understand and master the structure and use of Java language branch statements

2. Experimental content

1. Experimental steps:
⑴. Write a program that uses an if branch statement.
⑵. Write a program and use the switch branch statement.
⑶. Record the source program and running results, and compare the differences and respective advantages of branch statements.
2. Program and operation results:
⑴. Design and run the program
In this experiment, the grades of the classic 100-point system were selected and converted into 5 graded and classified topics, which were implemented with if-else nesting and switch statements.
if implements:

import java.util.Scanner;
public class Branch {
    
    
 	  public static void main(String[] args) {
    
    
 		   Scanner input = new java.util.Scanner(System.in);
		   System.out.print("请输入成绩:");
		   String s = input.next();
		   double d = Integer.parseInt(s);
		   if(d>=90)
		   System.out.println("A");
		   else if (d>=80)
		   System.out.println("B");
		   else if (d>=70)
		   System.out.println("C");
		   else if (d>=60)
		   System.out.println("D");
		   else
		   System.out.println("E"); 
		   input.close();
 	  }
}

Switch implementation:

  import java.util.Scanner;
public class Branch {
    
    
	   public static void main(String[] args) {
    
    
		Scanner input = new java.util.Scanner(System.in);
		System.out.print("请输入成绩:");
		String s = input.next();
		double d = Integer.parseInt(s);
		switch((int)(d/10)){
    
    
            case 10 :System.out.println("A");break; 
			case 9 :System.out.println("A");break; 
			case 8 :System.out.println("B");break;
			case 7 :System.out.println("C");break;
			case 6 :System.out.println("D");break;
			default :System.out.println("E");break; 	
		}
		input.close();
	  }
}

⑵. The screenshot of the experimental program running is as follows:
insert image description here
insert image description here

3. Experimental experience

**Through this experiment, I successfully learned about the branch structure if-else and switch structure of the java language.
The if-else structure has a selection function, and the corresponding program block is selected to be executed by judging the true or false of the condition. At the same time, we can also use if-else if-else for branch nesting. But it should be noted that when there is only one statement in the if statement block, "{}" can be omitted. And it is easy to make mistakes when the code changes in the future. Therefore, even if there is only one statement in the if statement block, do not omit "{}". It is very important to develop good programming habits.
The switch case structure is a special branch structure, which can be executed from different program entries according to the different values ​​of an integer expression. When the switch implements the branch function, the difference from if-else is that it has higher efficiency and a clearer structure. , more readable, but its use has certain restrictions, and you need to pay attention to the use of break.
Any complex program logic can be realized through the three basic program structures of "sequence", "branch", and "loop". I have benefited a lot from the study of this experiment.
**

Guess you like

Origin blog.csdn.net/qq_41866091/article/details/92385103