在这里,我们将解释如何编写一个 Java 程序来找出三个数中最大的数。首先,我们需要从用户那里读取三个整数,然后使用 if else 语句找出最大的数。
首先,我们需要声明 LargestNumber 类。然后,我们需要声明变量 a、b、c 来保存这三个整数。然后使用扫描器类对象从用户那里读取这些数字。通过使用 if 条件检查 if a > b 且 a > c,如果为真,则显示最大的数是 a。否则,检查 b > c。如果为真,则显示 b 是最大的数。否则,显示 c 是最大的数。
步骤 1:使用公共修饰符声明 LargestNumber 类。
步骤 2:打开 main() 以启动程序,Java 程序执行从 main() 开始
步骤 3:声明整数变量 a, b, c。
步骤 4:创建扫描器类对象 sc。
步骤 5:将三个数读入变量 a, b, c。
步骤 6:检查 a > b 且 a > c,如果为真,则显示最大的数是 a。
步骤 7:否则,检查 b > c,如果为真,则显示最大的数是 b。
步骤 8:否则,显示最大的数是 c。
import java.util.Scanner;
public class LargestNumber
{
public static void main(String[] args)
{
int a,b,c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
if(a > b && a > c)
{
System.out.println("The largest number is:"+a);
}
else if(b > c)
{
System.out.println("The largest number is:"+b);
}
else
{
System.out.println(" The largest number is:"+c);
}
}
}
Enter the first number:8 Enter the second number:0 Enter the third number:2 The largest number is:8