【无标题】

测试hasNextInt()和hasNextLine的区别

package com.yumingjiang.webdemo.lianxi;

import java.util.Scanner;

/**
 * @description: 测试hasNextInt()和hasNextLine的区别
 * 结论:前者键盘输入完后 回车后 可以继续再输入,但是后者一行数据输出完后,线程就结束了。
 * @author: John
 * @version: ScannerTest.java, v1.0 2024年05月13日 19:12
 */
public class ScannerTest {
    public static void main(String[] args) {
//        testNextInt();

        testNextLine();
    }

    private static void testNextLine(){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String nextLine = scanner.nextLine();
            System.out.println(nextLine);
        }
    }

    private static void testNextInt(){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            System.out.println(scanner.next());
        }
    }
}

相关推荐

  1. 标题

    2024-05-14 05:04:12       43 阅读
  2. 标题

    2024-05-14 05:04:12       44 阅读
  3. 标题

    2024-05-14 05:04:12       41 阅读
  4. 标题

    2024-05-14 05:04:12       48 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 05:04:12       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 05:04:12       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 05:04:12       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 05:04:12       18 阅读

热门阅读

  1. Rust语言中带标签的`break`用法

    2024-05-14 05:04:12       13 阅读
  2. 【C】每日一题 53 最大子数组和

    2024-05-14 05:04:12       9 阅读
  3. Excel中的`MMULT`函数

    2024-05-14 05:04:12       10 阅读
  4. Spring事务深度解析

    2024-05-14 05:04:12       12 阅读
  5. redis运维

    2024-05-14 05:04:12       13 阅读
  6. 23种设计模式(软考中级 软件设计师)

    2024-05-14 05:04:12       10 阅读
  7. .gitignore 文件的说明文档

    2024-05-14 05:04:12       11 阅读
  8. day5 qt

    day5 qt

    2024-05-14 05:04:12      15 阅读
  9. 重构四要素:目的、对象、时机和方法

    2024-05-14 05:04:12       14 阅读