Java 程序查找三个数中最大的数

编写一个 Java 程序,使用 Else If 语句、嵌套 If 和条件运算符来找出三个数中最大的数。有许多方法可以找出三个数中最大的数,但我们使用这些程序。

使用 Else If 语句查找三个数中最大数的 Java 程序

此程序帮助用户输入三个不同的值,并使用 Else If 语句找出这三个数中最大的数。

// using else if
package SimpleNumberPrograms;

import java.util.Scanner;

public class LargestUsingElseIf {

	private static Scanner sc;
	
	public static void main(String[] args) {
		int x, y, z;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter three Different Value: ");
		x = sc.nextInt();
		y = sc.nextInt();
		z = sc.nextInt();
		
		if (x > y && x > z) {
			System.out.format("\n% d is Greater Than both %d and %d", x, y, z);
		}
		else if (y > x && y > z) {
			System.out.format("\n% d is Greater Than both %d and %d", y, x, z);
		}	
		else if (z > x && z > y) {
			System.out.format("\n% d is Greater Than both %d and %d", z, x, y);
		}		
		else {
			System.out.println("\n Either any two values or all the three values are equal");
		}
	}
}

查找三个数中最大数的 Java 程序分析

  1. Else If 语句 中的第一个 if 条件会检查 x 是否大于 y 且 x 是否大于 z。如果条件为 True,则 x 大于 y 和 z。
  2. 第一个 Else if 条件会检查 y 是否大于 x 且 y 是否大于 z。如果条件为 True,则 y 大于 x 和 z。
  3. 第二个 Else if 条件会检查 z 是否大于 x 且 z 是否大于 y。如果条件为 True,则 z 大于 x 和 y。
  4. 如果以上所有条件都不满足,则表示它们相等。

输出 1:让我们输入值 x = 50,y = 75,z = 98

Java Program for Largest of Three Numbers using Else If 1

让我们输入值 15、5 和 4

 Please Enter three Different Value: 
15
5
4

 15 is Greater Than both 5 and 4

第三个输出

 Please Enter three Different Value: 
15
60
4

 60 is Greater Than both 15 and 4

输出 4

 Please Enter three Different Value: 
15
15
11

 Either any two values or all the three values are equal

使用嵌套 If 语句查找三个数中最大数的 Java 程序

此程序使用 嵌套 If 语句来找出这三个数中最大的数。

package SimpleNumberPrograms;

import java.util.Scanner;

public class LargUsingNestedIf {
	private static Scanner sc;

	public static void main(String[] args) {
		int x, y, z;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter three Different Value: ");
		x = sc.nextInt();
		y = sc.nextInt();
		z = sc.nextInt();
		
		LarOfTh(x, y, z);
	}
	public static void LarOfTh(int x, int y, int z) {
		if (x - y > 0 && x - z > 0) {
			System.out.format("\n% d is Greater Than both %d and %d", x, y, z);
		}
		else {
			if (y -z > 0) {
				System.out.format("\n% d is Greater Than both %d and %d", y, x, z);
			}
			else {
				System.out.format("\n% d is Greater Than both %d and %d", z, x, y);
			}
		}		
	}
}

查找三个数中最大数的程序分析

  1. 第一个 Java if 条件会检查 (x – y) 是否大于 0 且 (x – z) 是否大于 0。如果我们从大数减去小数,此条件将失败。否则,它将为 True。如果此条件为 True,则 x 大于 y 和 z。
  2. 当第一个 if 条件为 False 时,Else 语句将执行。因此,无需检查 x 值。在 Else 语句中,我们插入另一个 if 条件(嵌套 IF)来检查 (y – z) 是否大于 0。如果为 True,则 y 大于 x 和 z。
  3. 否则,z 大于 x 和 y。

输出:让我们输入值 x = 15,y = 87,z = 26。

Program for Largest of Three Numbers using Nested If Statement

使用三元运算符查找三个数中最大数的 Java 程序

程序 使用 三元运算符 来找出这三个数中最大的数。

package SimpleNumberPrograms;

import java.util.Scanner;

public class LagUsingConditionalOper {
	private static Scanner sc;

	public static void main(String[] args) {
		int x, y, z, lar;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter three Different Value: ");
		x = sc.nextInt();
		y = sc.nextInt();
		z = sc.nextInt();
		
		lar = ((x > y && x > z)? x: (y > z)?y:z);
			System.out.format("\n Largest number among three is: %d", lar);
	}
}

查找三个数中最大数的 Java 程序分析

  1. 在此示例中,第一个条件会检查 x 是否大于 y 且 x 是否大于 z。如果此条件为 True,它将返回 ? 符号后的第一个值,即变量 x(x 大于 y,z)。
  2. 如果第一个条件失败,它将执行 : 符号后的变量。我们在此处使用嵌套条件运算符检查另一个条件(y > z)。如果此条件为 True,它将返回 ? 符号后的第一个值,即变量 y(y 大于 x,z)。
  3. 如果嵌套条件失败,它将执行 : 符号后的变量,即变量 z。这意味着 z 大于 x 和 y。

输出 1:让我们输入值 15、6 和 9。

Program for Largest of Three Numbers using Ternary Operator

让我们输入不同的值

 Please Enter three Different Value: 
5
98
87

 Largest number among three is: 98

第三个输出

 Please Enter three Different Value: 
100
500
980

 Largest number among three is: 980

使用 OOP 查找三个数中最大数的 Java 程序

在此示例中,我们使用面向对象编程将代码进行划分。为此,我们将创建一个类,其中包含两个方法来找出两个数中较大的一个。

package SimpleNumberPrograms;

public class LargestNumber {
	int x, y, z, largest;
	
	public void LOT() {
		if (x - y > 0 && x - z > 0) {
			System.out.format("\n% d is Greater Than both %d and %d", x, y, z);
		}
		else {
			if (y -z > 0) {
				System.out.format("\n% d is Greater Than both %d and %d", y, x, z);
			}
			else {
				System.out.format("\n% d is Greater Than both %d and %d", z, x, y);
			}
		}
	}
	
	public int LOTNumber(int x, int y, int z) {
		largest = ((x > y && x > z)? x: (y > z)?y:z);
		return largest;	
	}
}

在“查找三个数中最大数的 Main 程序”中,我们将创建上述类的实例并调用这些方法。

package SimpleNumberPrograms;

import java.util.Scanner;

public class LargestUsingClass {

	private static Scanner sc;

	public static void main(String[] args) {
		int a, b, c, largest;
		sc = new Scanner(System.in);	
	
		System.out.println("\n Please Enter three Different Value: ");
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		
		LargestNumber ln = new LargestNumber();
		ln.x = a;
		ln.y = b;
		ln.z = c;
		ln.LOT();
		
		largest = ln.LOTNumber(a, b, c);
		System.out.format("\n Largest number among three is = %d", largest);
		
	}
}
 Please Enter three Different Value: 
45
75
25

 75 is Greater Than both 45 and 25
 Largest number among three is: 75

LargestNumber 类分析

  • 首先,我们声明了一个不带参数的 LOT 函数。然后,我们在函数中使用嵌套 If 语句来找出三个数中较大的一个。最后,我们在函数中使用 System.out.println 语句来打印输出。
  • 接下来,我们声明了一个带三个参数的整数函数 LOTNumbers。我们在上面的示例中已经解释了逻辑。

查找三个数中最大数的 Java 主类分析

在此程序中,我们创建了 LargestNumber 类的实例/对象。

LargestNumber ln = new LargestNumber();

接下来,我们将用户输入的值分配给 LargestNumber 类的变量。

ln.x = a;
ln.y = b;
ln.z = c;

接下来,我们调用 LOT 方法。请注意,这是我们创建的第一个 void 方法,它将找出三个数中最大的数,并从 LargestNumber 类本身打印输出。

ln.LOT();

接下来,我们通过传递三个参数来调用 LOTNumbers 方法。此函数将找出程序中三个数中最大的一个并返回较大的值。这就是为什么我们将输出分配给 largest 变量。

largest = ln.LOTNumber(a, b, c);

最后,System.out.println 语句将打印输出。