简体   繁体   English

如何在单个命令行中输入两个值?

[英]How can I input two values in a single command line?

I'm wondering how I can input two values through the terminal using only one command line. 我想知道如何只使用一个命令行通过终端输入两个值。 So instead of the user writing "5 {ENTER} 2 {ENTER}", it'd accept "5 2 {ENTER}" or "5, 2 {ENTER}". 因此,用户不是写“5 {ENTER} 2 {ENTER}”,而是接受“5 2 {ENTER}”或“5,2 {ENTER}”。

Basically, how to make this: 基本上,如何做到这一点:

System.out.println("Enter x value");
int x = inputScanner.nextInt();

System.out.println("Enter y value");
int y = inputScanner.nextInt();

into something that rather lets the user input both x and y on one line? 相反,让用户在一行上同时输入x和y?

You can add this to your main() method: 您可以将它添加到main()方法:

public static void main(String[] args){
    int x = Integer.parseInt(args[0]);
    int y = Integer.parseInt(args[1]);

    //do something with x and y
}

and when you run it from the command line: 当您从命令行运行它时:

java your_class_name 5 2;

now x = 5 and y = 2 现在x = 5y = 2

But i think you probably want to read the user input entered in 1 line. 但我想你可能想读一行输入的用户输入。 Then you can just do: 然后你可以这样做:

System.out.println("Enter x and y");
int x = inputScanner.nextInt();
int y = inputScanner.nextInt();

note that this will only work if user enters the input in the form 5 2 ie with a space in between. 请注意,这仅在用户以5 2形式输入输入时才有效,即两者之间有空格。 If you want a comma in between, ie 5,2 then you have to change the delimiter of the Scanner : 如果你想在两者之间使用逗号,即5,2那么你必须更改Scanner的分隔符:

System.out.println("Enter x and y");

inputScanner.useDelimiter(",");

int x = inputScanner.nextInt();
int y = inputScanner.nextInt();

Another way to do this is to use main's arguments variable. 另一种方法是使用main的arguments变量。 (I am going to assume it is a jar file) (我将假设它是一个jar文件)

static void main (String[] args)

when the user runs the program using the following command: 当用户使用以下命令运行程序时:

java -jar thisjar.jar 5 2 

the 5 and 2 will be stored into the String[]. 5和2将存储在String []中。 (arg[0] = 5) (arg[1] = 2). (arg [0] = 5)(arg [1] = 2)。 From there you can do: 从那里你可以做到:

int x = Integer.parseInt(arg[0]); //You have to parse the integer because it was stored as a string
int y = Integer.parseInt(arg[1]);

The way i would achieve this is to read in x and y into a String named xy and my then split them into a array like this. 我实现这一点的方法是将x和y读入名为xy的String中,然后我将它们拆分成这样的数组。

System.out.println("Enter x and y values: ");
input = new Scanner(System.in); // Type the input like "x, y" (5, 5)
String xy = input.nextLine(); // Reads the x and y value as a string
String[] str = xy.split(","); // Takes the comma out and puts both values in the array
System.out.println("x: " + str[0] + ", y: " + str[1]); // the out looks like "x: 5, y: 5"

The command line argument answers are the easiest way to go, but if you want to do other things with the program before prompting the user for input, you're going to need to go another route. 命令行参数答案是最简单的方法,但如果你想在提示用户输入之前用程序做其他事情,你将需要走另一条路线。 The way I would approach this is scan the input as a string, telling the user to separate the x and y values by a comma. 我接近这个的方法是将输入扫描为字符串,告诉用户用逗号分隔x和y值。 Then, you can process the input, splitting it between the comma into x and y strings and checking to make sure x and y are integers (or floats), converting them into their desired types, or raising an error if the input was entered wrong. 然后,您可以处理输入,将它在逗号分隔为x和y字符串并检查以确保x和y是整数(或浮点数),将它们转换为所需类型,或者如果输入输入错误则引发错误。 You would then have your two points for whatever the program is using them for. 无论程序使用它们,您都可以得到两点。 I used this approach in Python for a homework solver I used in my Algebra class last year. 我在Python中使用这种方法来获得去年在我的代数课程中使用的作业解算器。 The code for this is pretty trivial so I'll let you take care of that part. 这个代码非常简单,所以我会让你照顾那个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 中将单行整数作为输入? - How can I take a single line of integers as input in Java? 如何将用户输入的单行输出为多行? - How can I output a single line of user input into multiple lines? 如何在单个输入中输入超过两个单词的字符串? - how can i input string with more than two words in a single input? 如何在单行输入中检查两个相同的数字 - How to check for two identical numbers in a single line of input 如何从 Java 的单行输入中读取多个 Integer 值? - How to read multiple Integer values from a single line of input in Java? 如何使输入读出一行中的多个整数? - How can I make an input read out multiple integers on a single line? 我如何在 java 中使用逗号分隔在单行中获取多个输入 - how can i take multiple input in single line using coma separation in java 如何运行命令行FFMPEG并接受多个管道(视频和音频)而不会阻塞第一个输入? - How can I run command line FFMPEG and accept multiple pipes (video and audio) without blocking on the first input? 我怎样才能获得写在一行中的两个数字-是否有Split()方法? - How can I get two numbers that are written in a single line - is there a Split() method? 如何从用户一行获得两个String输入? - How can I get two String inputs from the user with a single line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM