JAVA-API概述-Scanner类键盘录入数据

image
image
image
image
image
image

代码一

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.next();
        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) {
        //1.创建Scanner对象
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入整数");
        int num =sc.nextInt();//10+回车换行
        System.out.println("请输入字符串");
        String s = sc.next();


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

posted @ 2022-11-28 16:51  NiceTwocu  阅读(25)  评论(0编辑  收藏  举报