Use java to input any three integers and output the maximum

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the first integer:");
        int a = input.nextInt();

        System.out.print("Please enter the second integer:");
        int b = input.nextInt();

        System.out.print("Please enter the third integer:");
        int c = input.nextInt();

        int max = a;
        if (b > max) {
            max = b;
        }
        if (c > max) {
            max = c;
        }

        System.out.println("The largest number is: " + max);
    }
}
 

In this program, we first create an Scannerobject to read input from the console. Then the three integers are read in sequence and stored in the variables a, band c.

Next, we use a ifstatement chain to compare the sizes of these three numbers, find the largest number, and store it in the variable max.

Finally, we use System.out.println()the method to print out the largest number.

2. The following is another implementation that can also be used to read three integers from the console and output the largest number:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[] nums = new int[3];
        for (int i = 0; i < 3; i++) {             System.out.print("Please enter the "th" + (i+1) + "th integer: ");             nums[i] = input.nextInt();         }


        int max = Arrays.stream(nums).max().getAsInt();

        System.out.println("The largest number is: " + max);
    }
}
 

In this program, we first create an Scannerobject to read input from the console. Then using a loop, three integers are read in sequence and stored in an array nums.

Next, we Arrays.stream()convert the array into a stream using the method and then max()find the maximum value in it using the method and store it in the variable max.

Finally, we use System.out.println()the method to print out the largest number.

The following is another implementation that can also be used to read three integers from the console and output the largest number:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the first integer:");
        int a = input.nextInt();

        System.out.print("Please enter the second integer:");
        int b = input.nextInt();

        System.out.print("Please enter the third integer:");
        int c = input.nextInt();

        int max = Math.max(Math.max(a, b), c);

        System.out.println("The largest number is: " + max);
    }
}

In this program, we first create an Scannerobject to read input from the console. Then the three integers are read in sequence and stored in the variables a, band c.

Next, we use Math.max()the method to compare the sizes of these three numbers, find the largest number, and store it in the variable max.

Finally, we use System.out.println()the method to print out the largest number.

Guess you like

Origin blog.csdn.net/lmrylll/article/details/129934984