Java用户输入
在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中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

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

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。
