📜  求三个数中最大的Java程序

📅  最后修改于: 2022-05-13 01:55:10.691000             🧑  作者: Mango

求三个数中最大的Java程序

问题陈述:给定三个数 x、y 和 z,其中的目标是在这三个数中取最大。

例子:

Input: x = 7, y = 20, z = 56
Output: 56                    // value stored in variable z

3个数字中最大的流程图:

求三个数中最大者的算法:

1. Start
2. Read the three numbers to be compared, as A, B and C
3. Check if A is greater than B.

  3.1 If true, then check if A is greater than C
         If true, print 'A' as the greatest number
          If false, print 'C' as the greatest number

  3.2 If false, then check if B is greater than C
        If true, print 'B' as the greatest number
        If false, print 'C' as the greatest number
4. End

方法:

  • 使用三元运算符
  • 使用 if-else

方法一:使用三元运算符

条件运算符的语法:

ans = (conditional expression) ? execute if true : execute if false
  • 如果条件为真则执行冒号前的语句
  • 如果条件为假,则在冒号后执行语句,所以
largest = z > (x>y ? x:y) ? z:((x>y) ? x:y); 

插图:

x = 5, y= 10, z = 3

largest  = 3>(5>10 ? 5:10) ? 3: ((5>10) ? 5:10);
largest  = 3>10 ? 3 : 10
largest  = 10
Java
// Java Program to Find the Biggest of 3 Numbers
 
// Importing generic Classes/Files
import java.io.*;
 
class GFG {
   
    // Function to find the biggest of three numbers
    static int biggestOfThree(int x, int y, int z)
    {
 
        return z > (x > y ? x : y) ? z : ((x > y) ? x : y);
    }
 
    // Main driver function
    public static void main(String[] args)
    {
 
        // Declaring variables for 3 numbers
        int a, b, c;
 
        // Variable holding the largest number
        int largest;
        a = 5;
        b = 10;
        c = 3;
        // Calling the above function in main
        largest = biggestOfThree(a, b, c);
 
        // Printing the largest number
        System.out.println(largest
                           + " is the largest number.");
    }
}


Java
// Java Program to Find the Biggest of 3 Numbers
 
// Importing generic Classes/Files
import java.io.*;
 
class GFG {
 
    // Function to find the biggest of three numbers
    static int biggestOfThree(int x, int y, int z)
    {
 
        // Comparing all 3 numbers
        if (x >= y && x >= z)
 
            // Returning 1st number if largest
            return x;
 
        // Comparing 2nd no with 1st and 3rd no
        else if (y >= x && y >= z)
 
            // Return z if the above conditions are false
            return y;
 
        else
 
            // Returning 3rd no, Its sure it is greatest
            return z;
    }
 
    // Main driver function
    public static void main(String[] args)
    {
        int a, b, c, largest;
 
        // Considering random integers three numbers
        a = 5;
        b = 10;
        c = 3;
        // Calling the function in main() body
        largest = biggestOfThree(a, b, c);
 
        // Printing the largest number
        System.out.println(largest
                           + " is the largest number.");
    }
}



输出
10 is the largest number.

方法 2:使用 if-else 语句

在这种方法中,if-else 语句将通过比较数字来比较和检查最大的数字。 'If' 将检查 'x' 是否大于 'y' 和 'z'。 'else if' 将检查 'y' 是否大于 'x' 和 'z'。如果两个条件都为假,那么“z”将是最大的数字。

Java

// Java Program to Find the Biggest of 3 Numbers
 
// Importing generic Classes/Files
import java.io.*;
 
class GFG {
 
    // Function to find the biggest of three numbers
    static int biggestOfThree(int x, int y, int z)
    {
 
        // Comparing all 3 numbers
        if (x >= y && x >= z)
 
            // Returning 1st number if largest
            return x;
 
        // Comparing 2nd no with 1st and 3rd no
        else if (y >= x && y >= z)
 
            // Return z if the above conditions are false
            return y;
 
        else
 
            // Returning 3rd no, Its sure it is greatest
            return z;
    }
 
    // Main driver function
    public static void main(String[] args)
    {
        int a, b, c, largest;
 
        // Considering random integers three numbers
        a = 5;
        b = 10;
        c = 3;
        // Calling the function in main() body
        largest = biggestOfThree(a, b, c);
 
        // Printing the largest number
        System.out.println(largest
                           + " is the largest number.");
    }
}


输出
10 is the largest number.