# java foundation -- 3if statement programming problem

Posted by cypher235 on Wed, 09 Mar 2022 09:50:00 +0100

# java foundation - 3if statement programming problem

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

1. What knowledge points can be used to judge the prices of different purchase methods?

### Problem solving scheme

1. Use the if... else statement to determine

### Operation steps

1. Calculate the cost of trade in without use.
2. Calculate the cost of trade in.
3. Use the if... else statement to determine which method is more economical and output the result

```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

1. How to complete user entry?
2. What knowledge points are needed to find the minimum value?

### Problem solving scheme

1. Use Scanner to complete keyboard entry, and use the nesting of if... else to complete judgment

2. 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

1. Use the method of three keyboard entries to let the user enter three integers
2. Use the multi conditional if... else to judge various situations of the size relationship of three numbers and find the minimum value
3. Printout minimum

```//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

1. How do you allow users to enter content?
2. Which if statement format is used to judge the information?

### Problem solving scheme

1. Nested operations using Scanner and if... else.

### Operation steps

1. Enter the amount and year on the keyboard.
2. Use multi condition if... else to judge the year and calculate the principal and interest amount.
3. Output results.

```//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

1. How do you allow users to enter content?
2. Which if statement format is used to judge the information?

### Problem solving scheme

1. Use Scanner keyboard entry and if... else judgment statements to complete the nesting.

### Operation steps

1. Enter the member category and shopping amount with the keyboard.
2. First use if else to judge the customer category.
3. In different customer categories, the if else statement is used to judge the shopping amount.
4. Output results.

```//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("The amount payable to you 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");
}
}
}
```

Topics: Java