Home > Java > javaTutorial > body text

How to output the maximum value of three numbers in java

王林
Release: 2020-04-29 11:15:44
Original
17629 people have browsed it

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);
	}
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template