Knowledge points
if judgment statement
Topic 1 (training)
Li Lei wants to buy a new mobile phone with a value of 7988 yuan. Her old mobile phone can sell for 1500 yuan in the second-hand market, and the mobile phone store offers the discount of exchanging the old for the new. If she gives her old mobile phone to the store, the new mobile phone can get a 20% discount. In order to save more money, should Li Lei trade in the old for the new? Please output in the console.
Training tips
- What knowledge points can be used to judge the prices of different purchase methods?
Problem solving scheme
- Use the if... else statement to determine
Operation steps
- Calculate the cost of trade in without use.
- Calculate the cost of trade in.
- Use the if... else statement to determine which method is more economical and output the result
Reference answer
public class Demo1 { public static void main(String[] args) { //1. Calculate the cost of replacing the old with the new int money1 = 7988 - 1500; //2. Calculate the cost of trade in double money2 = 7988 * 0.8; //3. Two ways to judge if(money1 > money2){ System.out.println("Using old for new saves money"); }else{ System.out.println("It's more economical not to use the old for the new"); } } }
Topic 2 (training)
Let the user enter three integers in turn, calculate the minimum value of the three numbers, and print it to the console.
Training tips
- How to complete user entry?
- What knowledge points are needed to find the minimum value?
Problem solving scheme
-
Use Scanner to complete keyboard entry, and use the nesting of if... else to complete judgment
-
Use Scanner to complete keyboard entry, and use multi conditional if... else to complete judgment
The following solutions are subject to scheme 2
Operation steps
- Use the method of three keyboard entries to let the user enter three integers
- Use the multi conditional if... else to judge various situations of the size relationship of three numbers and find the minimum value
- Printout minimum
Reference answer
//1. Guide Package import java.util.Scanner; public class Demo2 { public static void main(String[] args) { //2. Create keyboard entry object Scanner sc = new Scanner(System.in); //3. Enter three numbers respectively System.out.println("Please enter the first integer:"); int i = sc.nextInt(); System.out.println("Please enter the second integer:"); int j = sc.nextInt(); System.out.println("Please enter the third integer:"); int k = sc.nextInt(); //4. Define variables to represent the minimum value int min; //5. Judge three integers if( i < j && i < k){ min = i; }else if( j < i && j < k){ min = j; }else{ min = k; } //6. Print minimum value System.out.println("The minimum value is" + min); } }
Topic 3 (comprehensive)
A bank has launched the fixed-term deposit business of lump sum deposit and withdrawal. Its deposit period is divided into one year, two years, three years and five years, and the principal and interest are withdrawn by the certificate of deposit at maturity. The annual interest rate of deposits is as follows:
Annual interest rate of deposit period (%)
2.25 a year
Two years 2.7
Three years 3.25
Five years 3.6
Please deposit a certain amount (1000 deposits) and a certain number of years (one out of four), and calculate the total principal and interest after maturity.
Tips:
The deposit amount and deposit period are entered by keyboard
Calculation method of principal and interest: principal + principal × Annual interest rate × Years
Training tips
- How do you allow users to enter content?
- Which if statement format is used to judge the information?
Problem solving scheme
- Nested operations using Scanner and if... else.
Operation steps
- Enter the amount and year on the keyboard.
- Use multi condition if... else to judge the year and calculate the principal and interest amount.
- Output results.
Reference answer
//1. Guide Package import java.util.Scanner; public class Demo3 { public static void main(String[] args) { // 2. Create keyboard entry object Scanner sc = new Scanner(System.in); // 3. Call the method to obtain the principal and deposit and withdrawal years entered by the keyboard System.out.println("Please enter the deposit amount:"); int money = sc.nextInt(); System.out.println("Please enter the deposit period:"); int year = sc.nextInt(); // 4. Variables defining principal and interest double outMoney = 0; // 5. Calculate the principal and interest according to the interest rate and service life if (year == 1) { outMoney = money + money * 2.25 / 100 * 1; } else if (year == 2) { outMoney = money + money * 2.7 / 100 * 2; } else if (year == 3) { outMoney = money + money * 3.25 / 100 * 3; } else if (year == 5) { outMoney = money + money * 3.6 / 100 * 5; } else { System.out.println("The number of years entered is incorrect"); } // 6. Printout System.out.println("deposit" + year + "The principal and interest after years are:" + outMoney); } }
Topic 4 (comprehensive)
A shopping mall can offer discounts. The specific rules are as follows:
Ordinary customers will not get a discount if they buy less than 100 yuan, and 10% off if they buy more than 100 yuan;
20% off for members who buy less than 200 yuan and 7.5% off for members who buy more than 200 yuan;
Different discount rules are not cumulative.
Please make shopping settlement according to this preferential plan, enter the customer category (0 for ordinary customers, 1 for members) and the pre discount amount of shopping (integer), and output the payable amount (decimal type).
Training tips
- How do you allow users to enter content?
- Which if statement format is used to judge the information?
Problem solving scheme
- Use Scanner keyboard entry and if... else judgment statements to complete the nesting.
Operation steps
- Enter the member category and shopping amount with the keyboard.
- First use if else to judge the customer category.
- In different customer categories, the if else statement is used to judge the shopping amount.
- Output results.
Reference answer
//1. Guide Package import java.util.Scanner; public class Demo4 { public static void main(String[] args) { // 2. Create keyboard entry object Scanner sc = new Scanner(System.in); // 3. Call the method to obtain the customer category and shopping amount entered by the keyboard System.out.println("Please enter the customer category (0 for ordinary customers and 1 for member customers):"); int type = sc.nextInt(); System.out.println("Please enter the purchase amount:"); int money = sc.nextInt(); // 4. Judge the customer category first if (type == 0) { // 4.1. Ordinary customers, and then judge the shopping amount if (money > 0 && money < 100) { System.out.println("Your payable amount is:" + money); } else if (money >= 100) { System.out.println("Your payable amount is:" + money * 0.9); } else { System.out.println("The amount you entered is incorrect"); } } else if (type == 1) { // 4.2. Member customers, and then judge the shopping amount if (money > 0 && money < 200) { System.out.println("Your payable amount is:" + money * 0.8); } else if (money >= 200) { System.out.println("Your payable amount is:" + money * 0.75); } else { System.out.println("The amount you entered is incorrect"); } } else { System.out.println("The customer category you entered is incorrect"); } } } ```# Knowledge points switch Judgment statement ## Topic 1 (comprehensive) Analog calculator function, two functions of keyboard input int Type of data for addition, subtraction, multiplication and division, and print the operation results. requirement: Enter three integers on the keyboard,The first two integers represent the data participating in the operation, and the third integer is the operation to be performed(1:Represents an addition operation,2:Represents subtraction,3:Represents multiplication,4:Representation division operation),The demonstration effect is as follows: Please enter the first integer: 30 Please enter the second integer: 40 Please enter the operation you want to perform(1:Addition representation,2:Representation subtraction,3:Represents multiplication,4:Representation Division): 1 console output :30+40=70 ### Training tips 1. After the user enters the data, what knowledge points are used to judge the four different operations of addition, subtraction, multiplication and division? ### Problem solving scheme 1. use switch Judge the completion of the statement. ### Operation steps 1. Use the keyboard to enter three variables. 2. use switch The judgment statement judges the third variable and matches the operation to be performed. 3. In every case Perform different operations on the first variable and the second variable respectively. ### Reference answer ```java // 1. Guide Package import java.util.Scanner; public class Demo2 { public static void main(String[] args) { // 2. Create keyboard entry object Scanner sc = new Scanner(System.in); // 3. Prompt the data to be input and call the method to obtain the input data System.out.println("Please enter the first integer:"); int num1 = sc.nextInt(); System.out.println("Please enter the second integer:"); int num2 = sc.nextInt(); System.out.println("Please enter the operation you want to perform(1:Indicates addition,2:Representation subtraction,3:Represents multiplication,4:Representation Division)"); int type = sc.nextInt(); // 4. Use the switch statement to judge the calculation type and output the corresponding results switch (type) { case 1: System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); break; case 2: System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); break; case 3: System.out.println(num1 + " * " + num2 + " = " + (num1 * num2)); break; case 4: System.out.println(num1 + " / " + num2 + " = " + (num1 * 1.0 / num2)); break; default: System.out.println("The operation category you entered is incorrect"); break; } } }