Scanner练习

练习1 键盘输入两个数字求和

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入第一个数字");
        int number1 = in.nextInt();

        System.out.println("请输入第二个数字");
        int number2 = in.nextInt();

        int sum = number1 + number2;

        System.out.println(number1 + "+" + number2 + "=" + sum);

    }

练习2 键盘输入3个数字求出最大值

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入第一个数字");
        int number1 = in.nextInt();

        System.out.println("请输入第二个数字");
        int number2 = in.nextInt();

        System.out.println("请输入第三个数字");
        int number3 = in.nextInt();

        int temp = number1 > number2 ? number1:number2;
        int max = temp > number3 ? temp : number3;
        System.out.println("您输入的数中最大值为"+max);

    }
}
posted @ 2022-06-29 14:23  我滴妈老弟  阅读(24)  评论(0编辑  收藏  举报