gpt4 book ai didi

java - 谁能告诉我 read() 和 readline(); 之间有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:06:06 25 4
gpt4 key购买 nike

package com.learn.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BufferReaderInput {

public static void main(String[] args) throws IOException {
BufferedReader bufferreaderIn = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter your Name");
String Name = bufferreaderIn.readLine();

System.out.println("Enter your age");
int age = Integer.parseInt(bufferreaderIn.readLine());

System.out.println("Enter your salary");
int sal = bufferreaderIn.read();

System.out.println("Hi, I'm " + Name + " my age is " + age
+ " and my salary is " + sal);
}

}

当我使用 obj.read(); 输入薪水时,它没有给出正确的输出。

谁能告诉我 read()readline() 有什么区别?

最佳答案

来自 documentation BufferedReader,我们为 read 准备了这个:

Reads a single character.

这是 readLine :

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

因此,区分一行何时被视为终止的细节,不同之处在于第一个读取单个字符,而第二个读取整行。

关于java - 谁能告诉我 read() 和 readline(); 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com