首页 > Java > java教程 > 正文

Java用户输入

WBOY
发布: 2024-08-30 16:06:42
原创
437 人浏览过

在Java程序中,我们可以通过3种方式在命令行环境下读取用户的输入来获取用户输入,Java BufferedReader类、Java Scanner类和Console类。让我们详细讨论一下课程。我们使用 Scanner 类来获取用户输入。该程序要求用户输入一个整数、一个字符串和浮点数,然后将其打印在显示屏上。 java.util 中存在扫描器类,因此我们可以将此包添加到我们的软件中。首先,我们创建一个 Scanner 类对象并使用 Scanner 类方法。

Java 用户输入的 3 种方式

可以通过三种方式读取用户输入:

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

  1. Java BufferedReader 类
  2. Java 扫描器类
  3. 使用控制台类

这三个类在下面提到;让我们详细讨论它们:

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);
}
}
登录后复制

输出:

Java用户输入

从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);
}
}
登录后复制

输出:

Java用户输入

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:

Java用户输入

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用户输入

以上是Java用户输入的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!