Branch statements mainly include the following structures:
If, if else, multiple if, nested if, switch
Let's explain the structure and use examples of each statement in detail.
1,if
// Simple if structure /* * if(Condition) { * content * } */ int num = 3; if (num >= 0) { System.out.println("if structure"); } if(false == 2>=4){ System.out.println("1"); }
2,if-else
// If else structure /* * if(Condition) { * content * }else{ * content * } */ int rank = 2; if (rank==1) { System.out.println("There are rewards!"); }else { System.out.println("Give you a slap!"); }
3. Multiple if
// If else if else structure /* * if(Condition 1) { * Content 1 * }else if(Condition 2) { * Content 2 * }else if(Condition 3) { * Content 3 * }else{ * Content 4 * } */ if(a==0) { System.out.println("a=0"); }else if(a==1) { System.out.println("a==1"); }else if(a==2){ System.out.println("a=2"); }else{ System.out.println("a>2"); }
4, nested if
//Nested if statement /* * if(Condition 1) { * if(Condition 2) { * content * } * } */ if (isFull==false) { if (a==2) { System.out.println("nesting if"); } }
5,switch
// switch statement /* * switch(Variables) { * case Value 1: * Content 1 * break; * case Value 2: * Content 2 * break; * default: * Content 3 * break; * } */ switch (a) { case 0: System.out.println(0); break; case 1: System.out.println(1); break; case 2: System.out.println(2); break; default: System.out.println("Nothing."); break; }
Here are a few examples of personal exercises to use:
Exercise 1
Title Description:
If you have more than 500w, you can buy a house within the 4th Ring Road.
If you have 200w to 500w, you can buy a house in the 4th Ring Road to the 5th Ring Road.
If you have 100w to 200w, you can buy a house outside the Fifth Ring Road.
If you can rent between 10000 and 100w,
If it's less than 1 W, you'll have to sleep on the street...
Exercise 2
At the beginning of the weekly cleaning, the teacher made the following arrangements for the students:
Boys more than one meter seven carry water, girls more than one meter seven clean glass, boys less than one meter seven pier, girls less than one meter seven clean table
Please write a program to assign the working groups of class members according to the requirements
Exercise 1 reference:
public class Exercise06 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int money = in.nextInt(); if (money>=5000000) { System.out.println("Buy a house within the Fourth Ring Road!"); }else if (money >= 2000000) { System.out.println("Four to five rings"); }else if (money >= 1000000) { System.out.println("Outside the Fifth Ring Road"); }else if (money >= 10000) { System.out.println("Renting"); }else { System.out.println("Sleeping on the streets"); } } }
Exercise 2 reference:
public class Exercise07 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("please input height:"); int height = in.nextInt(); System.out.println("please input sex:"); String sex = in.next(); if (height>=170) { if (sex.equals("male")) { System.out.println("Men more than one meter and seven should carry water"); }else{ System.out.println("Women over one meter and seven should clean the glass"); } }else { if (sex.equals("male")) { System.out.println("Let's go to Dundi for men under one meter seven"); }else{ System.out.println("Women under one meter seven should clean the table"); } } } }
Welcome to guide the exchange!