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!