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);
    }
}

 

posted @ 2021-07-13 19:41  凝眸望天涯  阅读(64)  评论(0)    收藏  举报