Case 1: two tigers
Demand: there are two tigers in the zoo. The weight of the two tigers is 180kg and 200kg respectively. Please use the program to judge whether the weight of the two tigers is the same
Analysis: 1. Define two variables to save the tiger's weight, in kg, which only reflects the value here. 2
2. Use the ternary operator to judge the weight of the tiger. If the weight is the same, return true; otherwise, return false
3. Output results
package test; public class test1 { public static void main(String[] args) { // 1. Define two variables to save the tiger's weight in kg, which only reflects the value here int tigerWeight1 = 180; int tigerWeight2 = 200; // 2. Use the ternary operator to judge the weight of the tiger. If the weight is the same, return true; otherwise, return false boolean result = tigerWeight1 == tigerWeight2 ? true : false; // 3. Output results System.out.println("The weight comparison between the two tigers is"+result); } }
Case 2: three monks
Demand: there are three monks living in a temple. Their heights are 150cm, 210cm and 165cm respectively. Please use the program to obtain the maximum height of the three monks
Analysis: 1. Define three variables to save the monk's height in cm, which only reflects the value here. 2
2. Use the ternary operator to obtain the higher height of the first two monks and save it with a temporary height variable
3. Use the ternary operator to obtain the temporary height value and the height higher value of the third monk, and save it with the maximum height variable
4. Output results
package test; public class test2 { public static void main(String[] args) { // 1. Define three variables to save the monk's height. The unit is cm. Only the value can be reflected here int monkHeight1 = 150; int monkHeight2 = 210; int monkHeight3 = 165; // 2. Use the ternary operator to obtain the higher height of the first two monks and save it with a temporary height variable int tempHeight = monkHeight1 > monkHeight2 ? monkHeight1 : monkHeight2; // 3. Use the ternary operator to obtain the temporary height value and the height higher value of the third monk, and save it with the maximum height variable int maxHeight = tempHeight > monkHeight3 ? tempHeight : monkHeight3; // 4. Output results System.out.println("The height of the tallest of the three monks is"+maxHeight+"cm"); } }
Case 3: upgraded version of three monks
Requirement: there are three monks living in a temple. Their height must be determined by Ze Liang. Please use the program to obtain the maximum height of the three monks
Analysis: 1. Unknown height, input with keyboard. First import the package, then create the object
2. Enter three heights on the keyboard and assign them to three variables respectively
3. Use the ternary operator to obtain the higher height value of the first two monks and save it with a temporary height variable
4. Use the ternary operator to obtain the temporary height value and the height higher value of the third monk, and save it with the maximum height variable
5. Output results
package test; import java.util.Scanner; public class test3 { public static void main(String[] args) { // 1. If the height is unknown, enter it by keyboard. First import the package, then create the object Scanner sc = new Scanner(System.in); // 2. Enter three heights on the keyboard and assign them to three variables respectively System.out.println("Please enter the height of the first monk:"); int monkHeight1 = sc.nextInt(); System.out.println("Please enter the height of the second monk:"); int monkHeight2 = sc.nextInt(); System.out.println("Please enter the height of the third monk:"); int monkHeight3 = sc.nextInt(); // 3. Use the ternary operator to obtain the higher height value of the first two monks and save it with a temporary height variable int tempHeight = monkHeight1 > monkHeight2 ? monkHeight1 : monkHeight2; // 4. Use the ternary operator to obtain the temporary height value and the height higher value of the third monk, and save it with the maximum height variable int maxHeight = tempHeight > monkHeight3 ? tempHeight : monkHeight3; // 5. Output results System.out.println("The height of the tallest of the three monks is"+maxHeight+"cm"); } }