Getting started with java | branch structure

Starting at: Getting started with java | branch structure java entry series, from scratch!!! The last issue is the exp...
Branching structure

Starting at: Getting started with java | branch structure
java entry series, from scratch!!!

The last issue is the explanation of data types, mainly the introduction of identifiers, keywords, comments, variables and constants, as well as the introduction of basic types and reference types in data types

This is the fifth issue: it's an explanation of the branch structure. We can understand and master the four branch structures by implementing if, if (judgment statement) else , else if and switch case

Branching structure

1,if

Although the program of sequential structure can solve the problems of calculation and output, it can't be judged and selected again.
The branch structure should be used for the problem of judgment before selection.

1.1 form Single branch If (judging condition) Multibranched If (judgment condition){ Code that meets the conditions }else{ Code that does not meet the conditions } Nested branch If (judgment condition 1){ Code 1 }Else if (judgment condition 2){ Code 2 }Else if (judgment condition 3){ Code 3 }Else if (judgment condition 4){ Code 4 }else{ Code that no one is satisfied with } 1.2 exercise 1: set a value within 10 and guess the number
package cn.qile; import java.util.Scanner; public class Test_If { public static void main(String[] args) { System.out.print("Please enter a number within 10:"); int a = new Scanner(System.in).nextInt(); if (a == 5) { System.out.print("congratulations! Guess right, the answer is 5!!!"); } } }
1.3 exercise 2: supplement the previous exercise. Only when the output is correct can the output be correct. If the output is wrong, the error will also be reported
package cn.qile; import java.util.Scanner; public class Test2_IfElse { public static void main(String[] args) { System.out.print("Please enter a number within 10:"); int a = new Scanner(System.in).nextInt(); if (a == 5) { System.out.print("congratulations! Guess right, the answer is 5!!!"); } else { System.out.print("Young man, you guessed wrong, come again next time!!"); } } }
1.4 exercise 3: product discounts

Receive the original price entered by the user.
10% off for 1000.
20% off for over 2000.
50% off for 5000.

package cn.qile; import java.util.Scanner; public class Test3_ElseIf { public static void main(String[] args) { //1. Receive the original price entered by the user System.out.print("Original price of goods:"); double price = new Scanner(System.in).nextDouble(); //2. Judge and calculate the discounted price double now = price;//Record discounted price if(price >= 5000) {//Up to 5000 now = price*0.5;//50% off }else if(price >= 2000) {//Over 2000 now = price*0.8;//20% off }else if(price >= 1000) {//Up to 1000 now = price*0.9;//10% off } System.out.println("The discounted price is:"+now); } }

2,switch

When a case is established, all cases, including default, are penetrated backward from the case.
It doesn't end until the program ends or a break program is encountered.

2.1 form
switch(Integer expression){ case 1 : syso(1);break; case 2 : syso(2); case 3 : syso(3); case 4 : syso(4); case 5 : syso(5); default:syso(0); }
2.2 exercise 4: number matching
package cn.qile; public class Test4_Switch { public static void main(String[] args) { int a = 1; //1. The parameter type in parentheses is integer: byte short int char jdk1.5 followed by String switch(a) { //3. When case is matched and there is no break, all cases including default will be penetrated backward //4. break can end the program case 1 : System.out.println(1);break; case 'a' : System.out.println(2);break; case 20 : System.out.println(3);break; //2. If there is no matching case, default will be executed default:System.out.println(0); } } }

Next issue: getting started with java (6) definition and use of method
Pay attention to [Qile is not a code farm], reply to [java introduction], watch the next issue in advance!!

Happy waiting for you

15 June 2020, 00:57 | Views: 8537

Add new comment

For adding a comment, please log in
or create account

0 comments