Java 程序用于查找三个数字中的最大值
javaobject oriented programmingprogramming更新于 2024/4/12 11:56:17
在本文中,我们将了解如何在 Java 中查找三个整数中的最大值。这是使用大于运算符 (<) 完成的。这里我们使用一个简单的 if-else 条件来比较值。
下面是相同的演示 −
输入
假设我们的输入是 −
-50, 30 和 50
输出
所需的输出将是 −
最大的数字是 50
算法
步骤 1- 开始 步骤 2- 声明三个整数:input_1、input_2 和 input_3 步骤 3- 提示用户输入三个整数值/定义整数 步骤 4- 读取值 步骤 5- 使用 if else 循环,将第一个输入与其他两个输入进行比较以检查它是否是 三个整数中最大的一个。如果不是,则对其他两个整数重复此步骤。 步骤 6- 显示结果 步骤 7- 停止
示例 1
import java.util.Scanner;
public class Largest {
public static void main(String[] args) {
int my_input_1, my_input_2, my_input_3;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A scanner object has been defined ");
System.out.print("Enter the first number : ");
my_input_2 = my_scanner.nextInt();
System.out.print("Enter the second number : ");
my_input_2 = my_scanner.nextInt();
System.out.print("Enter the third number : ");
my_input_3 = my_scanner.nextInt();
my_input_1 = -50;
my_input_2 = 30;
my_input_3 = 50;
if( my_input_1 >= my_input_2 && my_input_1 >= my_input_3)
System.out.println("The largest number is " +my_input_1);
else if (my_input_2 >= my_input_1 && my_input_2 >= my_input_3)
System.out.println("The largest number is " +my_input_2);
else
System.out.println("The largest number is " +my_input_3);
}
}
输出
Required packages have been imported A reader object has been defined Enter the first number : -50 Enter the second number : 30 Enter the third number : 50 The largest number is 50
示例 2
这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。
public class Largest {
public static void main(String[] args) {
int my_input_1, my_input_2, my_input_3;
my_input_1 = -50;
my_input_2 = 30;
my_input_3 = 50;
System.out.println("The three numbers are defined as " +my_input_1 +", " +my_input_2 +" and " +my_input_3);
if( my_input_1 >= my_input_2 && my_input_1 >= my_input_3)
System.out.println("The largest number is " +my_input_1);
else if (my_input_2 >= my_input_1 && my_input_2 >= my_input_3)
System.out.println("The largest number is " +my_input_2);
else
System.out.println("The largest number is " +my_input_3);
}
}
输出
The three numbers are defined as -50, 30 and 50 The largest number is 50
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

