zoukankan      html  css  js  c++  java
  • Scanner

    Scanner 方便我们操作 System.in, 因为 System.in 本身是 inputStream.

    基本语法是:

    Scanner scan = new Scanner(System.in); // 这样相当于我打开了一个对象, 这个对象一直可以接收来自标准输入的值, 保存在scan对象中.

    即便是程序运行期间, 这个对象也还是在等待着接收用户的输入. 这点与传统 C 语言不同, 传统C 语言需要使用一个函数, 比如 Input(), 每次调用这个函数才接收一次用户输入.

    而这个 Scanner 相当于说一直等待着输入. 

    如果是字符串, 使用 next() 或 nextLine(), 但是操作之前要先判断 hasNext, hasNextLine

    举例: nextLine

    import java.util.Scanner;
     
    public class ScannerDemo {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            // 从键盘接收数据
     
            // nextLine方式接收字符串
            System.out.println("nextLine方式接收:");
            // 判断是否还有输入
            if (scan.hasNextLine()) {
                String str2 = scan.nextLine();
                System.out.println("输入的数据为:" + str2);
            }
            scan.close();
        }
    }

    next() 与 nextLine() 区别

    next():

    • 1、一定要读取到有效字符后才可以结束输入。
    • 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
    • 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
    • next() 不能得到带有空格的字符串。

    nextLine():

    • 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
    • 2、可以获得空白。

    如果要 Int 或 float 类型数据, 也有支持, 但是最好之前使用 hasNextInt, 或者类似 hasNextFloat 判断之后再赋值, 否则会抛异常.

    package Example200;
    
    import java.util.Scanner;
    
    public class LeapYear {
    
        public static void main(String[] args) {
            // Scan处理输入, System.in 是真正接收磁盘输入的, 而且是秩序等待监听
            Scanner scan = new Scanner(System.in);
            int year = 0;
            if (scan.hasNextInt()) {
                year = scan.nextInt();
            }
            if (year % 4 ==0 && year % 100 != 0 || year % 400 == 0) {
                System.out.println(year + " 是闰年");
            } else {
                System.out.println(year + " 不是闰年");
            }    
        }
    }
    package com.test.leon;
    
    import java.util.Scanner;
    
    public class TestScanner {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int year = 0;
            while (true) {
                if (scanner.hasNextInt()) {
                    year = scanner.nextInt();
                    if (year >= 1900) {
                        break;
                    } else {
                        System.out.println("Please input > 1900");
                    }
                }
            }
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                System.out.println(year + " yes");
            } else {
                System.out.println(year + " no");
            }
            
            scanner.close();
        }
    
    }
  • 相关阅读:
    浅谈移动通信和无线通信
    jdk8处理时间
    Mysql查询报错:Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
    linux系统执行mysql脚本:Can't connect to local MySQL server through socket '/tmp/mysql.sock'
    org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to C:UsersADMINI~1AppDataLocalTemp2
    java实现每个单词首字母大写
    常用Java技术社区
    Hibernate处理事务并发问题
    Hibernate的检索策略
    Java对象在Hibernate持久化层的状态
  • 原文地址:https://www.cnblogs.com/moveofgod/p/12941015.html
Copyright © 2011-2022 走看看