其他分享
首页 > 其他分享> > Scanner练习

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

    }
}

标签:Scanner,int,练习,System,number2,number1,println,out
来源: https://www.cnblogs.com/ailhy/p/16423252.html