在Java程序中,我们可以通过3种方式在命令行环境下读取用户的输入来获取用户输入,Java BufferedReader类、Java Scanner类和Console类。让我们详细讨论一下课程。我们使用 Scanner 类来获取用户输入。该程序要求用户输入一个整数、一个字符串和浮点数,然后将其打印在显示屏上。 java.util 中存在扫描器类,因此我们可以将此包添加到我们的软件中。首先,我们创建一个 Scanner 类对象并使用 Scanner 类方法。
Java 用户输入的 3 种方式
可以通过三种方式读取用户输入:
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
- Java BufferedReader 类
- Java 扫描器类
- 使用控制台类
这三个类在下面提到;让我们详细讨论它们:
1. Java BufferedReader 类
它扩展了读者类别。 BufferedReader 从字符输入流读取输入并缓冲字符,以便高效读取所有输入。默认大小较大,适合缓冲。当用户发出任何读取请求时,相应的请求就会发送给读取器,读取器发出字符或字节流的读取请求;因此,BufferedReader 类包装在另一个输入流(例如 FileReader 或 InputStreamReaders)周围。
例如:
BufferedReader reader = new BufferedReader(new FileReader("foo.in"));
BufferedReader 可以使用 readLine() 方法逐行读取数据。
BufferedReader 可以让代码的性能更快。
构造函数
BufferedReader 有两个构造函数,如下:
1。 BufferedReader(Reader reader):用于创建使用输入缓冲区默认大小的缓冲输入字符流。
2。 BufferedReader(Reader reader, input size):用于创建缓冲输入字符流,该流使用为输入缓冲区提供的大小。
功能
- int read:用于读取单个字符。
- int read(char[] cbuffer, int offset, int length): 用于读取数组指定部分的字符。
- String readLine (): 用于逐行读取输入。
- boolean ready(): 用于测试输入缓冲区是否准备好读取。
- 长跳过:用于跳过字符。
- void close():它关闭输入流缓冲区和与该流关联的系统资源。
当用户从键盘输入字符时,它会被设备缓冲区读取,然后从 System.in 传递到缓冲读取器或输入流读取器并存储在输入缓冲区中。
代码:
import java.util.*; import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /*package whatever //do not write package name here */ class BufferedReaderDemo { public static void main (String[] args) throws NumberFormatException, IOException { System.out.println("Enter your number"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); System.out.println("Number you entered is: " + t); System.out.println("Enter your string"); String s = br.readLine(); System.out.println("String you entered is: " + s); } }
输出:
从InputStreamReader和BufferedReader读取的程序:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderDemo { public static void main(String args[]) throws IOException{ InputStreamReader reader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(reader); System.out.println("What is your name?"); String name=br.readLine(); System.out.println("Welcome "+name); } }
输出:
2. Java 扫描器类
java.util。 Scanner 类是用于从键盘读取用户输入的类之一。它可以在 util 包中找到。扫描器类使用默认情况下主要是空格的分隔符来中断用户输入。扫描器有很多方法可以读取许多基本类型的控制台输入,例如 double、int、float、long、Boolean、short、byte 等。这是 java 中获取输入的最简单的方法。 Scanner 类实现 Iterator 和 Closeable 接口。扫描器提供了 nextInt() 和许多原始类型方法来读取原始类型的输入。 next() 方法用于字符串输入。
Constructors
- Scanner(File source): It constructs a scanner to read from a specified file.
- Scanner(File source, String charsetName): It constructs a scanner to read from a specified file.
- Scanner(InputStream source), Scanner(InputStream source, String charsetName): It constructs a scanner to read from a specified input stream.
- Scanner(0Readable source): It constructs a scanner to read from a specified readable source.
- Scanner(String source): It constructs a scanner to read from a specified string source.
- Scanner(ReadableByteChannel source): It constructs a scanner to read from a specified channel source.
- Scanner(ReadableByteChannel source, String charsetName): It constructs a scanner to read from a specified channel source.
Functions
Below are mentioned the method to scan the primitive types from console input through Scanner class.
- nextInt(),
- nextFloat(),
- nectDouble(),
- nextLong(),
- nextShort(),
- nextBoolean(),
- nextDouble(),
- nextByte(),
Program to read from Scanner Class:
Using scanner class. import java.util.Scanner; /*package whatever //do not write package name here */ class ScannerDemo { public static void main (String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your number"); int t = sc.nextInt(); System.out.println("Number you entered is: " + t); System.out.println("Enter your string"); String s = sc.next(); System.out.println("String you entered is: " + s); } }
Output:
3. Using console Class
Using the console class to read the input from the command-line interface. It does not work on IDE.
Code:
public class Main { public static void main(String[] args) { // Using Console to input data from user System.out.println("Enter your data"); String name = System.console().readLine(); System.out.println("You entered: "+name); } }
Output:
以上是Java用户输入的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

禅工作室 13.0.1
功能强大的PHP集成开发环境