• JAVA笔试面试必备输入输出语句


    1. 前言

    • 在Java笔试面试中,经常会碰到ACM格式,因此对于一些常用的输入输出操作应该烂熟于心。

    2. 基本的输入输出Java语法

    2.1 next()、nextLine()、nextInt()辨析

    • 首先应该记住该方法的包名:

    import java.util.Scanner;

    1.next()、nextLine()、nextInt() 的相同点:

    next()、nextLine()、nextInt()是scanner内置的方法,遇到 空格,Tab,Enter,会继续等待,直到获得相应类型相应的值。而nextLine() 会把空格,Tab,Enter 视为输入。

    2.next()、nextLine()、nextInt()的区别

    1.使用 next() 方法时,将空格看作 是两个字符串的 间隔,返回值是字符串类型的。
    2.使用 nextLine() 方法时,不将空格看做 是两个字符串的 间隔,而是看作字符串的一部分,返回时,它作为String类型一并返回
    3.使用 nextInt() 方法时,与 next() 方法类似,依旧 将空格看作 是两个输入的数据的 间隔,只是它的返回值是 int类型 的,
    (当使用nexInt()方法时,只能输入int类型的数据。)

    import java.util.Scanner;
    
    /**
     * @author 草原一只鹰
     * @create 2022-05-12 11:32
     */
    public class Main {
        public static void main(String[] args) {
            //测试哪种方法打开哪种方法的注释即可
            //1.测试next()方法
            //String next = inputTestnext();
            //将输入的字符串进行输出,遇到空格,Tab,Enter,会继续等待,直到获得相应类型相应的值
            //System.out.println(next);
    
            //2.测试nextLine()方法
            //String nextline = inputTestnextLine();
            //System.out.println(nextline);
    
            //3.测试nextInt()方法
            int nextInt = inputTestnextInt();
            System.out.println(nextInt);
    
    
        }
    
        public static String inputTestnext() {
            Scanner sc = new Scanner(System.in);
            //假如输入字符串: a [空格一下] 1 -> 输出:a1
            String next = sc.next();
            return next;
        }
    
        public static String inputTestnextLine() {
            Scanner sc = new Scanner(System.in);
            //假如输入字符串: b [空格一下] 2  -> 输出:b 空格 2
            String nextline = sc.nextLine();
            return nextline;
        }
    
        public static int inputTestnextInt() {
            Scanner sc = new Scanner(System.in);
            //假如输入字符串: [空格一下] 2  -> 输出:2
            int nextInt = sc.nextInt();
            return nextInt;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3. 改进的输入输出语句

    3.1 BufferReader

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    str = reader.readLine();从这个缓存中读取一行的内容

    • 详解:

      1. System.in 接受从控制台输入的字节
      1. new InputStreamReader(System.in);构造一个InputStreamReader对象这个对象是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
      1. new BufferedReader(new InputStreamReader(System.in));构造一个字符流的缓存,里面存放在控制台输入的字节转换后成的字符。
      1. str = reader.readLine();从这个缓存中读取一行的内容, 注意:包含空格。
    • BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


    • 整个这句话拆开来写为:

    InputStream in = System.in ;

    InputStreamReader isr = new InputStreamReader(in);

    BufferedReader reader = new BufferedReader(isr);

  • 相关阅读:
    一个简单证件照的设计过程
    竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)
    【无标题】
    Opencore 常见kext驱动详解
    线性混合模型(Linear Mixed Models)与R语言 lmer() 函数
    Git详解及 github与gitlab使用
    振弦采集模块的通讯速率和软件握手( UART)
    6-Dubbo架构设计与底层原理-服务导出源码分析(下)
    CC1101一款低成本的sub- 1ghz 收发器 芯片
    Spring三级缓存解决循环依赖
  • 原文地址:https://blog.csdn.net/qq_46214369/article/details/124727294