在 Java 中从 System.in 读取输入
本文介绍如何使用 Java 中的 System.in
从控制台读取用户输入。
Java 提供了一个低级流类 System
来读取用户输入,它使用输入流来读取输入。System
是 Java 中的一个类,可帮助执行与系统相关的任务。
我们可以将它传递给 Scanner
类,然后使用它的方法;我们可以获得多种类型的用户输入,例如 String
、int
、float
等。让我们通过一些例子来理解。
在 Java 中使用 System.in
读取输入
在 Java 代码中使用 System.in
很容易;在 Scanner
构造函数中传递类并使用 nextLine()
方法。此方法读取并返回一个字符串。
请参见下面的示例。
import java.util.Scanner;
public class SimpleTesting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a value :");
String str = sc.nextLine();
System.out.println("User input: " + str);
}
}
输出:
Enter a value :
2
User input: 2
使用 Java 中的 System.in
和 BufferedReader
类读取输入
这是读取用户输入的另一种解决方案,我们使用 BufferedReader
类而不是 Scanner
类。这段代码执行相同的任务,我们在这里使用 readLine()
方法来读取数据。
此方法属于 BufferedReader
类并返回一个字符串。请参见下面的示例。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SimpleTesting {
public static void main(String[] args) throws IOException {
System.out.println("Enter a value :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(str);
}
}
输出:
Enter a value :
sam
sam
在 Java 中使用 System.console()
方法读取输入
Java System
类提供了 console()
方法来处理与控制台相关的任务。所以,要读取数据,我们也可以使用这种方法。
此方法返回一个控制台对象,我们可以通过该对象调用 readLine()
方法来读取数据。请参见下面的示例。
import java.io.Console;
import java.io.IOException;
public class SimpleTesting {
public static void main(String[] args) throws IOException {
Console c = System.console();
System.out.println("Enter a value : ");
String str = c.readLine();
System.out.println(str);
}
}
输出:
Enter a value :
sam
sam
Java Scanner
类通常用于读取用户数据并为每种数据类型提供方法。
我们可以使用这些方法来读取特定的数据。其中一些如下。
public int nextInt(); // reads integer input
public float nextFloat(); // reads decimal input
public String nextLine(); // reads string input
在下面的示例中,我们使用这些方法来读取 Java 中不同类型的用户输入。它将帮助你了解 Java 控制台。
请参见下面的示例。
import java.io.IOException;
import java.util.Scanner;
public class SimpleTesting {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string value : ");
String str = sc.nextLine();
System.out.println(str);
System.out.println("Enter an int value : ");
int a = sc.nextInt();
System.out.println(a);
System.out.println("Enter a float value : ");
float f = sc.nextFloat();
System.out.println(f);
}
}
输出:
Enter a string value :
string
string
Enter an int value :
23
23
Enter a float value :
34
34.0
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。