Scanner03Max

时间:2021-07-13
本文章向大家介绍Scanner03Max,主要包括Scanner03Max使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package cn.yyhl.day07;

import java.util.Scanner;

/*
题目:
键盘输入三个int数字,然后求出其中的最大值。

思路:
1需要键盘输入就要用到Scanner类。
2需要三个int变量接收键盘输入的数字
3还需要一个变量仅限接收比较之后的最大值。
4打印输出最大值
 */
public class Scanner03Max {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个数字 ");
        int a = sc.nextInt();
        System.out.println("请输入第二个数字 ");
        int b = sc.nextInt();
        System.out.println("请输入第三个数字 ");
        int c = sc.nextInt();
        
        //第一种写法
        /*int max = 0;
        if ( a > b){
            max = a;
        }
        else {
            max = b;
        }
        if (max > c){
            System.out.println("三个数中的最大值是 " + max);
        }
        else {
            System.out.println("三个数中的最大值是 " + c);
        }*/
        
        //第二种写法
        int temp = a > b ? a : b;
        int max = temp > c ? temp : c;
        System.out.println("三个数中的最大值是 " + max);
    }
}

原文地址:https://www.cnblogs.com/langwuzhang/p/15008125.html