首先,让我们声明并初始化三个long值。
long val1 = 88799; long val2 = 98567; long val3 = 98768;
现在,使用以下条件找到三个长值中的最小值:
//检查最大值 if (val2 < val1) { val1 = val2; } if (val3 < val1) { val1 = val3; }
以下是获取最小值的完整示例-
public class Demo { public static void main(String[] args) { long val1 = 88799; long val2 = 98567; long val3 = 98768; //显示 System.out.println("Number 1 = "+val1); System.out.println("Number 2 = "+val2); System.out.println("Number 3 = "+val3); //检查最大值 if (val2 < val1) { val1 = val2; } if (val3 < val1) { val1 = val3; } System.out.println("The minimum of three numbers: "+val1); } }
输出结果
Number 1 = 88799 Number 2 = 98567 Number 3 = 98768 The minimum of three numbers: 88799