Home >Java >javaTutorial >How to determine whether two tigers have the same weight in Java
1. Question:
There are two tigers in the zoo. It is known that the weight of the two tigers is 180kg and 200kg respectively. Please use the program to determine the two tigers. are of the same weight.
2. Analysis:
Define two variables to save the weight of the tiger (unit is kg, only the numerical value is reflected here)
Use the ternary operator to determine the weight of the tiger. If the weight is the same, return true, otherwise return false
Output result
3. Practice:
The code is as follows (example):
public class Demo1 { public static void main(String[] args) { //不爱生姜不吃醋 //定义两个变量用于保存老虎的体重(单位为kg,这里只体现数值即可) int weigth2=180; int weight2=200; //用三元运算符实现老虎体重的判断,体重相同,返回true,否则返回false boolean b= weigth2 == weight2 ? true:false; //输出结果 System.out.println(b); } }
The above is the detailed content of How to determine whether two tigers have the same weight in Java. For more information, please follow other related articles on the PHP Chinese website!