美文网首页
next()、nextInt()和nextLine()区别

next()、nextInt()和nextLine()区别

作者: 有事找叮当 | 来源:发表于2020-12-18 20:12 被阅读0次

键盘录入字符串

Scanner类 :

​ next() : 遇到了空格, 就不再录入数据了 , 结束标记: 空格, tab键

​ nextLine() : 可以将数据完整的接收过来 , 结束标记: 回车换行符

代码实现 :

package com.itheima.api;

import java.util.Scanner;

public class Demo1Scanner {
    /*
        next() : 遇到了空格, 就不再录入数据了

                结束标记: 空格, tab键

        nextLine() : 可以将数据完整的接收过来

                结束标记: 回车换行符
     */
    public static void main(String[] args) {
        // 1. 创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入:");
        // 2. 调用nextLine方法接收字符串
        // ctrl + alt + v : 快速生成方法的返回值
        String s = sc.nextLine();

        System.out.println(s);
    }
}

package com.itheima.api;

import java.util.Scanner;

public class Demo2Scanner {
    /*
        nextInt和nextLine方法配合使用的时候, nextLine方法就没有键盘录入的机会了

        建议: 今后键盘录入数据的时候, 如果是字符串和整数一起接受, 建议使用next方法接受字符串.
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入整数:");
        int num = sc.nextInt(); // 10 + 回车换行
        System.out.println("请输入字符串:");
        String s = sc.nextLine();


        System.out.println(num);
        System.out.println(s);
    }
}

相关文章

  • next()、nextInt()和nextLine()区别

    键盘录入字符串 Scanner类 : ​ next() : 遇到了空格, 就不再录入数据了 , 结束标记: 空...

  • Scanner用法

    一、常用方法next()、nextInt()和nextLine()的使用总结 例一 输出结果: nextInt()...

  • Java中Scanner类的nextLine、nextInt、n

    问题描述当nextLine放置在next、nextInt之后,在输入第一行数据的时候,会自动跳过nextLine,...

  • JAVA方法笔记

    字符串输入.next()和.nextLine()的区别 nextLine()方法返回的是Enter键之前的所有字符...

  • 刷题踩坑笔记

    1、nextInt()和nextLine()连用(输入总是少一行) 问题分析: nextLine()会把nextI...

  • nextLine()和next()、nextInt()等类型的区

    nextLine()只以Enter键作为结束,即可以读取有效字符和无效字符(Tab、空格)。next()只能读取有...

  • Next和nextLine()的区别

    Next和nextLine()的区别?在Scanner对象中,next读取时,遇到空格就结束,会自动过滤到有效字符...

  • java循环语句

    一、循环语句 1.Scanner(next()和nextInt())和Random(nextInt())的使用 代...

  • first

    输入 输出 in.nextLine()读取in,根据具体内容可使用nextInt()或nextFloat()自动补...

  • scanner next和scanner nextLine的区别

    该问题源于浏览StackOverflow时遇到的一个提问 问题链接[https://stackoverflow.c...

网友评论

      本文标题:next()、nextInt()和nextLine()区别

      本文链接:https://www.haomeiwen.com/subject/muufnktx.html