• Java键盘录入案例


    • 键盘录入

      • Java里面有一个类叫Scanner,可以接收键盘输入的数字
      • 第一步:导包,找Scanner这个类

        • import java.util.Scanner;
        • 导包必须在类定义的上边(public class上面)
      • 第二步:创建对象,开始使用Scanner这个类

        • Scanner sc = new Scanner(System.in);
        • sc为变量名可以改变,其他不能改
      • 第三步:接收数据

        • int i = sc.nextInt();
        • i为变量名可以改变,其他不能改
      • 注意:这种方式只能接收整数,输入字符小数会报错
        1. //1.导包
        2. import java.util.Scanner;
        3. public class HelloWorld {
        4. public static void main(String[] args) {
        5. //2.创建对象,表示我现在要用Scannner这个类
        6. Scanner sc = new Scanner(System.in);
        7. System.out.println("请在你的键盘上输入:");
        8. //3.接收数据;变量i记录键盘录入数据
        9. int i = sc.nextInt();
        10. System.out.println(i);
        11. }
        12. }

    • 进阶练习1:用键盘录入实现两数和
      1. //1.导包
      2. import java.util.Scanner;
      3. public class HelloWorld {
      4. public static void main(String[] args) {
      5. //2.创建对象,表示我现在要用Scannner这个类
      6. Scanner sc = new Scanner(System.in);
      7. System.out.println("请在你的键盘上输入第一个数:");
      8. //3.接收数据;变量:记录键盘录入数据
      9. int number1 = sc.nextInt();
      10. System.out.println("请在你的键盘上输入第一个数:");
      11. int number2 = sc.nextInt();
      12. int sum = number1 + number2;
      13. System.out.println("两数和为" + sum);
      14. }
      15. }

    • 进阶练习2:输入一个三位数,分别输出个位,十位,百位数字

      1. //1.导包
      2. import java.util.Scanner;
      3. public class index {
      4. public static void main(String[] args) {
      5. //2.创建对象,表示我现在要用Scannner这个类
      6. Scanner sc = new Scanner(System.in);
      7. System.out.println("请在你的键盘上输入一个三位数:");
      8. //3.接收数据;变量记录键盘录入数据
      9. int number = sc.nextInt();
      10. System.out.println("个位数是:" + number % 10);
      11. System.out.println("十位数是:" + number / 10 % 10);
      12. System.out.println("百位数是:" + number / 100 % 10);
      13. }
      14. }

    • 进阶练习3:你和你的对象正在试图餐厅获得一张桌子,键盘录入两个整数,表示你们衣服的时髦度(0~10之间,不能录入其他),如果你的时髦度大于你对象,那么相亲成功,输出true,否则输出false

      1. //1.导包
      2. import java.util.Scanner;
      3. public class index {
      4. public static void main(String[] args) {
      5. //2.创建对象,表示我现在要用Scannner这个类
      6. Scanner sc = new Scanner(System.in);
      7. System.out.println("请输入我们自己衣服的时髦度:");
      8. //3.接收数据;变量:记录键盘录入数据
      9. int myFashion = sc.nextInt();
      10. System.out.println("请输入我们对象衣服的时髦度:");
      11. int girlFashion = sc.nextInt();
      12. boolean result = myFashion > girlFashion;
      13. System.out.println(result);
      14. }
      15. }

    • 进阶练习4:键盘录入两个整数,如果其中一个为6,则最终输出true;如果他们的和是6的倍数,最终输出true,否则输出false

      1. //1.导包
      2. import java.util.Scanner;
      3. public class index {
      4. public static void main(String[] args) {
      5. //2.创建对象,表示我现在要用Scannner这个类
      6. Scanner sc = new Scanner(System.in);
      7. System.out.println("请输入第一个整数:");
      8. //3.接收数据;变量:记录键盘录入数据
      9. int number1 = sc.nextInt();
      10. System.out.println("请输入第二个整数:");
      11. int number2 = sc.nextInt();
      12. boolean result = number1 == 6 || number2 == 6 || (number1 + number2) % 6 == 0;
      13. System.out.println(result);
      14. }
      15. }

    • 进阶练习5:动物园里有两只老虎,请你输入两只老虎的体重,通过程序判断是否相同

      1. //1.导包
      2. import java.util.Scanner;
      3. public class index {
      4. public static void main(String[] args) {
      5. //2.创建对象,表示我现在要用Scannner这个类
      6. Scanner sc = new Scanner(System.in);
      7. System.out.println("请输入第一只老虎的体重:");
      8. //3.接收数据;变量:记录键盘录入数据
      9. int weight1 = sc.nextInt();
      10. System.out.println("请输入第二只老虎的体重:");
      11. int weight2 = sc.nextInt();
      12. String result = weight1 == weight2 ? "相同" : "不同";
      13. System.out.println(result);
      14. }
      15. }

  • 相关阅读:
    skywalking9.2.0源码修改
    WWDC 2024 回顾:Apple Intelligence 的发布与解析
    Go数据结构队列
    git远程仓库分支推送与常见问题
    为什么要为TypeScript添加类型支持?TypeScript优势
    upload-labs通关(Pass06-Pass10)
    JAVA计算机毕业设计高校设备采购审批管理系统Mybatis+源码+数据库+lw文档+系统+调试部署
    Java—hashCode、equals
    大数据管理系统架构Hadoop
    Gitter+Sidecar制作聊天室
  • 原文地址:https://blog.csdn.net/unravel_sky/article/details/132945326