Important content
1. Branch
Contents of logical operators left over from the previous article
Breaking principle of 0.0 logical operator
&&(and) | (or)! (non)
Circuit breaking principle of logic and:
Condition 1 & & condition 2 & & condition 3 as long as one of your conditions is false, the overall result is false
If condition 1 is false, condition 2 and condition 3 are not executed
Circuit breaking principle of logic or:
Condition 1 | condition 2 | condition 3 as long as one of your conditions is true, the overall result is true
If condition 1 is true, condition 1 and condition 2 are not executed.
If condition 1 is false, condition 2 is executed, condition 2 is true, and condition 3 is not executed.
/** * Logic breaking principle */ public class Demo1 { public static void main(String[] args) { int num = 10;//Declared a variable called num and assigned 10 to num //Condition 1 & & condition 2 //3>4 && ++num>10 //false && ++num //3> 4. False circuit breaking principle. If false occurs, the following conditions will not be implemented boolean ret = (3 > 4) && (++num>15); System.out.println(ret);//false System.out.println(num);//11 /* * ctrl+shift+? Shortcut keys for multiline comments * ctrl + ?Shortcut keys for single line notes * Circuit breaking principle of logic or * */ int num2 = 20; //++Num2 > 20 this code is not executed boolean ret1 = 4 > 3 || ++num2 > 20; System.out.println(ret1);//true System.out.println(num2);//20 } }
1. Branch
The branch of life: the choice of life
Branches in programs: also an option
1.1 if branch
Syntax format:
if (conditional){
Statement body
}
The condition must be Boolean data (true or false)
Execution process: if the condition is true, the contents in braces after the condition will be executed. If the condition is false, skip the contents in braces to execute the following code.
/** * if structure */ public class Demo2 { public static void main(String[] args) { int money = 9; if (money > 10) { System.out.println("You can buy a Lamborghini"); } System.out.println("Program end"); } }
1.2 if else branch
Syntax format:
if (conditional){
Statement body 1
} else {
Statement body 2
}
Execution process:
After the program runs to if else, first judge the condition. If the condition is true, execute the statement body 1
If the condition is false, execute statement body 2, you can only choose one of them.
/** * if-else Branching structure */ public class Demo3 { public static void main(String[] args) { int a = 19; if (a >= 10) { System.out.println("Have a good lunch today! Instant noodles with eggs"); } else { System.out.println("The northwest wind started"); } System.out.println("Program end"); } }
1.3 if else branch structure
Syntax format:
if (condition 1){
Statement body 1
}Else if (condition 2){
Statement body 2
}else if (condition 3){
Statement body 3
......
}Else if (condition n){
Statement body n
} else {
Statement body n+1
}
Execution process:
When running to if, first judge the conditions in parentheses. If the condition is tfalse, skip the statement body and execute the following else if conditions until the condition is true, then print the output. The following conditions need not be judged again.
/** * if-else if() structure */ public class Demo4 { public static void main(String[] args) { /* * Student achievement: * 90-100: Print Xiuer * 80-90: good * 70-80: commonly * 60-70: pass * 60 The following: call parents * * */ int score = 70;//First define a variable with a value of 70 if (score >= 90 && score <= 100) { System.out.println("Xiuer"); } else if (score >= 80) { System.out.println("good"); } else if (score >= 70) { System.out.println("commonly"); } else if (score >= 60) { System.out.println("pass"); } else { System.out.println("Call parents"); } } }
Another chestnut
import java.util.Scanner; public class Demo5 { public static void main(String[] args) { int score = 0; Scanner scanner = new Scanner(System.in); System.out.println("Pro, please enter your score:"); score = scanner.nextInt();//The console inputs data and assigns it to the score variable //Judgment of legitimacy if (score < 0 || score > 100) { System.out.println("Pro, you funny, lost wrong!!!"); //Exit program System.exit(0); } if (score >= 90 && score <= 100) { System.out.println("Xiuer"); } else if (score >= 80) { System.out.println("good"); } else if (score >= 70) { System.out.println("commonly"); } else if (score >= 60) { System.out.println("pass"); } else { System.out.println("Call parents"); } } }
1.4 switch case branch
It is also an option. You can perform the current operation as long as the current conditions are met
Switch (expression){
case constant 1:
Statement body 1;
break;
case constant 2:
Statement body 2;
break;
case constant 3:
Statement body 3;
break;
default:
Statement body n;
break;
}
break: stop, interrupt
import java.util.Scanner; public class Demo7 { public static void main(String[] args) { char c = '0'; Scanner scanner = new Scanner(System.in); System.out.println("My guest, please order!!!"); System.out.println("1.Roast whole pig"); System.out.println("2.Roasted Whole Lamb"); System.out.println("3.Roast Camel"); System.out.println("4.Roasted leek"); System.out.println("5.Roasted medlar"); c = scanner.nextLine().charAt(0); switch (c) { case '1': System.out.println("Roast the whole pig, fat and oily...."); break; case '2': System.out.println("Roast whole sheep, 2000 one"); break; case '3': System.out.println("Roast camels, only humps......"); break; case '4': System.out.println("Roast leek, never eaten!!!"); break; case '5': System.out.println("Roast Chinese wolfberry. This can make a fire"); break; default: System.out.println("We don't have this meal"); break; } } }
1.5 case 1
Please insert your card and output the password: 1.The password is correct After the password is correct, please select the business to handle: 1.withdraw money 2.deposit 3.Change Password 1 withdraw money 2 deposit 3 Change Password 4 There is no such business 2.Wrong password
import java.util.Scanner; //Small case of bank card withdrawal if else public class Demo9 { public static void main(String[] args) { int password = 123456;//Card password int account = 100000;//Cary's money System.out.println("Please insert your card and enter your password:"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt();//Password entered by console if (number == password) { //1 step for correct password //Please select the business to handle: 1 Withdrawal 2 # 3 change password System.out.println("Login succeeded!!!"); System.out.println("Please select the business to handle:1.Withdrawal 2.Deposit 3.Change Password"); Scanner scanner1 = new Scanner(System.in); int access = scanner1.nextInt();//Choose to handle business if (access == 1) { //withdraw money //Please enter the withdrawal amount System.out.println("Please withdraw the amount:"); Scanner scanner2 = new Scanner(System.in); int outMoney = scanner2.nextInt();//Amount withdrawn System.out.println("Your withdrawal amount is:" + outMoney); if (outMoney > account) { System.out.println("Sorry, your credit is running low"); }else { int account1 = account - outMoney;//Balance after current withdrawal System.out.println("The balance in your card is:" + account1); System.out.println("Withdrawal succeeded. Please withdraw your card. Welcome to come next time"); } }else if (access == 2) { //deposit System.out.println("Please enter the amount of your deposit:"); Scanner scanner2 = new Scanner(System.in); int inMoney = scanner2.nextInt();//Amount of deposit int account3 = account + inMoney; System.out.println("Your card balance is:" + account3); System.out.println("Deposit success, you go"); }else if (access == 3) { //Change Password System.out.println("Please enter the original password:"); Scanner scanner2 = new Scanner(System.in); int word = scanner2.nextInt();//Original password entered if (word == password) { System.out.println("Please enter a new password:"); Scanner scanner3 = new Scanner(System.in); int newPassword = scanner3.nextInt();//Enter your new password password = newPassword; System.out.println("Password modified successfully!!!"); }else { //Original password input error System.out.println("The original password is entered incorrectly. This card is frozen"); } }else { System.out.println("There is no such business"); System.exit(0);//Program exit } } else { //2 step of password error System.out.println("Password error, please re-enter"); } } }
1.6 case 2
Judge whether the entered year is a normal year or leap year
(Can be divided by 400) or (can be divided by 4 and cannot be divided by 100)
import java.util.Scanner; /** * Judge whether it is a normal year or a leap year */ public class Demo10 { public static void main(String[] args) { System.out.println("Please enter a year:"); Scanner scanner = new Scanner(System.in); int year = scanner.nextInt();//Enter a year for if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { System.out.println(year + "It's a leap year"); } else { System.out.println(year + "It's a normal year"); } } }
1.7 case 3
Three numbers, compare the size and output the largest number
import java.util.Scanner; //Compare the size of three different numbers and output the maximum number public class Demo11 { public static void main(String[] args) { System.out.println("Please enter three numbers:"); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); if (a > b && a > c) { System.out.println(a); } else if (b > a && b > c) { System.out.println(b); }else if(c > a && c > b) { System.out.println(c); } } }
1.8 case 4
Enter a month and print the corresponding season (spring, summer, autumn and winter)
import java.util.Scanner; public class Demo12 { public static void main(String[] args) { System.out.println("Please enter a month:"); Scanner scanner = new Scanner(System.in); int month = scanner.nextInt(); if (month <= 0 || month > 12) { System.out.println("The month entered is incorrect, please re-enter!!!"); System.exit(0); } if (month >= 3 && month <= 5) { System.out.println("spring"); }else if (month >= 6 && month <= 8) { System.out.println("summer"); }else if (month >= 9 && month <= 11) { System.out.println("autumn"); }else if (month == 12 || month <= 2 && month >= 1) { System.out.println("winter"); } } }
I have a few basic questions here. You can practice frequently. If you are proficient, you can also try multiple nesting (pay attention to the code specification!)
1. Define a variable of int type as X and assign a value of 5. If x < 10, let x increase automatically and output the last value of X.
2. Input a number a randomly. If a > 20, output the value of A.
3. Define an int variable with a score of 59. If the score is greater than or equal to 60, the output score will pass. Otherwise, the output score will fail.
4. Program and input integers a and b. If a or b is greater than 100, the difference between the sum of a and b and 100 will be output. Otherwise, the sum of the two numbers will be output.
5. Input the length of the three sides of the triangle from the keyboard to judge whether the three sides can form a triangle
6. Enter a number randomly to judge whether the number is odd or even;
7. Write a program to output the area of a circle;
8. Judge the number of days in the month according to the entered month
9. Enter the A, B and C levels of employee registration to judge whether they have a salary increase. The salary increase corresponds to 500, 300 and 100 (switch statement)
10. Enter a number 1-7 at will, judge the day of the week, and enter other numbers to prompt the input error. (write switch statement)
Supplementary knowledge points (use of Scanner)
Now when you declare a variable, you initialize the value directly. This value is written in our code.
Using Scanner, you can enter manually in the console and capture the entered values. After capturing the value, assign it to the variable.
1. You need to import Java util. Scanner;
2. Create a variable called scanner object
Scanner sc = new Scanner(System.in);
3. Use the sc object to obtain the data entered by the console and assign it to the variable
Get data of type int: int num= sc.nextInt();
Get data of float type: float f1 = sc.nextFloat();
Get data of double type: double d1 = sc.nextDouble();
Get string type data: String s1 = sc.nextLine();
Get character type data: char C1 = sc.nextline() charAt(0);
import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { //scanner is encapsulated by java //Console input Scanner scanner = new Scanner(System.in); //integer //System.out.println("pro!!! Please enter an integer:"); //scanner gets the integer entered by the console //int i = scanner.nextInt(); //decimal // System.out.println("pro!!! Please enter a decimal number:"); // //scanner gets the decimal number entered by the console // double v = scanner.nextDouble(); // System.out.println(v); //Gets the content of the string, with a space in the middle // System.out.println("please enter a string:"); // String s = scanner.nextLine(); // System.out.println(s); //Get the contents of the string. No spaces are allowed in the middle // System.out.println("please enter a string:"); // String next = scanner.next(); // System.out.println(next); //Get characters from console System.out.println("Please enter characters:"); //In order char c = scanner.nextLine().charAt(0); System.out.println(c); } }
import java.util.Scanner; public class Demo5 { public static void main(String[] args) { int score = 0; Scanner scanner = new Scanner(System.in); System.out.println("Pro, please enter your score:"); score = scanner.nextInt();//The console inputs data and assigns it to the score variable //Judgment of legitimacy if (score < 0 || score > 100) { System.out.println("Pro, you funny, lost wrong!!!"); //Exit program System.exit(0); } if (score >= 90 && score <= 100) { System.out.println("Xiuer"); } else if (score >= 80) { System.out.println("good"); } else if (score >= 70) { System.out.println("commonly"); } else if (score >= 60) { System.out.println("pass"); } else { System.out.println("Call parents"); } } }