迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中从 System.in 读取输入

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍如何使用 Java 中的 System.in 从控制台读取用户输入。

Java 提供了一个低级流类 System 来读取用户输入,它使用输入流来读取输入。System 是 Java 中的一个类,可帮助执行与系统相关的任务。

我们可以将它传递给 Scanner 类,然后使用它的方法;我们可以获得多种类型的用户输入,例如 Stringintfloat 等。让我们通过一些例子来理解。


在 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.inBufferedReader 类读取输入

这是读取用户输入的另一种解决方案,我们使用 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便