• Java数据输入


    数据输入

    • 1.1 数据输入概述
      数据输入

    • 1.2 Scanner使用的基本步骤

      • 1、导包
        • import java.util.Scanner;
        • 导包的动作必须出现在类定义的上边
      • 2、创建对象
        • Scanner sc = new Scanner(System.in);
        • 上面这个格式里面,只有sc是变量名,可以变,其他的都不允许变。
      • 3、接收数据
        • int i = sc.nextInt();
        • 上面这个格式里面,只有 i 是变量名,可以变,其他的都不允许变。
    • 案例:三个和尚

      • 需求: 一座寺庙里面住着三个和尚,他们的身高必须经过测量得出,请用程序实现获取这三个和尚的最高身高。
      • 分析:
        • 1、身高未知,采用键盘录入实现。首先导包,然后创建对象。
          • import java.util.Scanner;
          • Scanner sc = new Scanner(System.in);
        • 2、键盘录入三个身高分别赋值给三个变量。
          • int height1 = sc.nextInt();
          • int height2 = sc.nextInt();
          • int height3 = sc.nextInt();
        • 3、用三元运算符获取前两个和尚的较高身高值,并用临时射高变量保存起来。
          • (height1 > height2) ? height1 : height2;
        • 4、用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。
          • (tempHeight > height3) ? tempHeight : height3;
      • 代码实操
        • import java.util.Scanner;
        • public class OperatorTest01 {
          • pubilc static vioid main (String[] args) {
            • Scanner sc = new Scanner(System.in);
            • System.out.println(“请输入第一个和尚的身高:”)
            • int height1 = sc.nextInt();
            • System.out.println(“请输入第二个和尚的身高:”)
            • int height2 = sc.nextInt();
            • System.out.println(“请输入第三个和尚的身高:”)
            • int height3 = sc.nextInt();
            • int tempHeight = height1 > height2 ? height1 : height2;
            • int maxHeight = tempheight > height3 ? tempHeight : hieght3;
            • System.out.println(“这三个和尚中身高最高的是:” + maxHeight + “cm”);
          • }
        • }
  • 相关阅读:
    安装Ant 保姆级别教程
    极端业务场景下,我们应该如何做好稳定性保障?
    大语言模型RAG-技术概览 (一)
    数组指针、二级指针传参
    姓名缘分查询易语言代码
    11个销售心理学方法,帮你搞定客户!
    JWT的应用
    Global Mapper栅格计算器,波段计算NDVI、NDSI、NDWI等
    Leetcode 370、1109、1094差分数组考点
    springboot整合logback
  • 原文地址:https://blog.csdn.net/weixin_61427044/article/details/127680478