更新時(shí)間:2022-06-09 11:15:42 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1252次
在 Java 中,有四種不同的方式可以在命令行環(huán)境(控制臺(tái))中讀取用戶的輸入。
這是Java經(jīng)典的取輸入方式,在JDK1.0中引入。此方法通過將 System.in(標(biāo)準(zhǔn)輸入流)包裝在 InputStreamReader 中來使用,InputStreamReader 包裝在 BufferedReader 中,我們可以在命令行中讀取用戶的輸入。
輸入被緩沖以實(shí)現(xiàn)高效讀取。
包裝代碼很難記住。
執(zhí)行:
// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args)
throws IOException
{
// Enter data using BufferReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
// Reading data using readLine
String name = reader.readLine();
// Printing the read line
System.out.println(name);
}
}
輸入:
動(dòng)力節(jié)點(diǎn)
輸出:
輔助空間:O(1)
動(dòng)力節(jié)點(diǎn)
這可能是接受輸入的最首選方法。Scanner 類的主要目的是使用正則表達(dá)式解析原始類型和字符串,但是,它也可用于在命令行中讀取用戶的輸入。
從標(biāo)記化輸入中解析基元(nextInt()、nextFloat()、...)的便捷方法。
正則表達(dá)式可用于查找標(biāo)記。
閱讀方式不同步
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
class GetInputFromUser {
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string " + s);
int a = in.nextInt();
System.out.println("You entered integer " + a);
float b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
輸入:
GeeksforGeeks
12
3.4
輸出:
您輸入了字符串 GeeksforGeeks
您輸入了整數(shù) 12
你輸入了 float 3.4
它已成為從命令行讀取用戶輸入的首選方式。此外,它可以用于讀取類似密碼的輸入,而不用回顯用戶輸入的字符;也可以使用格式字符串語法(如 System.out.printf())。
優(yōu)點(diǎn):
讀取密碼而不回顯輸入的字符。
讀取方法是同步的。
可以使用格式字符串語法。
不適用于非交互環(huán)境(例如 IDE)。
// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
public class Sample {
public static void main(String[] args)
{
// Using Console to input data from user
String name = System.console().readLine();
System.out.println("You entered string " + name);
}
}
輸入:
GeeksforGeeks
輸出:
您輸入了字符串 GeeksforGeeks
最常用于競(jìng)爭(zhēng)性編碼的用戶輸入。命令行參數(shù)以字符串格式存儲(chǔ)。Integer 類的 parseInt 方法將字符串參數(shù)轉(zhuǎn)換為 Integer。同樣,對(duì)于執(zhí)行期間的浮動(dòng)和其他。args[] 的用法出現(xiàn)在這種輸入形式中。信息的傳遞發(fā)生在程序運(yùn)行期間。命令行提供給 args[]。這些程序必須在 cmd 上運(yùn)行。
代碼:
// Program to check for command line arguments
class Hello {
public static void main(String[] args)
{
// check if length of args array is
// greater than 0
if (args.length > 0) {
System.out.println(
"The command line arguments are:");
// iterating the args array and printing
// the command line arguments
for (String val : args)
System.out.println(val);
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
命令行參數(shù):
javac GFG1.java
java Main Hello World
輸出:
命令行參數(shù)是:
你好
世界
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743