Home  >  Article  >  Java  >  How to output the maximum value of three numbers in java

How to output the maximum value of three numbers in java

王林
王林Original
2020-04-29 11:15:4417543browse

How to output the maximum value of three numbers in java

Purpose:

Receive three integers from the console, and then find the maximum value among the three numbers.

Recommended video tutorial: java video

Implementation code:

package day03;
//输入三个int数中的最大值
import java.util.Scanner;
public class Text01 {
    public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请依次输入三个整数:a,b,c(以空格隔开)");
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
        //方法一
		int d=a>b?a:b;
        int max=d>c?d:c;
        System.out.println("最大值为:"+max);
        //方法二
        if (a>b && a>c) {
        	System.out.println("最大值为"+a);
        }else if(b>c && b>a) {
        	System.out.println("最大值为"+b);
        }else if(c>b && c>a) {
        	System.out.println("最大值为"+c);
        }else {
        	System.out.println("出现异常");
        }
	   //方法三
        int e=Math.max(c, Math.max(a, b));
        System.out.println("最大值为"+e);
	}
}

Recommended tutorial: java entry program

The above is the detailed content of How to output the maximum value of three numbers in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn