简体   繁体   English

如何在 Java 的同一行中将两个或一个整数作为输入?

[英]How can I take either two or one integer as input on the same line in Java?

For example, how can I take the either values 1 0 or just the value 2 to then be used at a latter part of the program.例如,如何取值1 0或仅取值2然后在程序的后期使用。 Say that 1 adds the number 0 to a list and 2 prints out a phrase.假设1将数字0添加到列表中,而2打印出一个短语。 Right now I only know how to take two numbers on the same line using现在我只知道如何使用在同一行上取两个数字

Scanner sc = new Scanner(System.in); // receives input for x y
int x = Integer.parseInt(sc.next());
int y = Integer.parseInt(sc.next());

Just put spaces between terms:只需在术语之间放置空格:

Scanner scanner = new Scanner(System.in);
int[] values = new int[2];
String[] arguments = scanner.nextLine().split(" ");
for(int i = 0; i < arguments.length && i < values.length; i++){
    values[i] = Integer.parseInt(arguments[i]);
}
scanner.close();

On line 2 you specify maximum inputs在第 2 行,您指定最大输入

On line 3 in the "" you can specify what goes between terms在“”的第 3 行,您可以指定术语之间的内容

On line 2 & 5 you can specify term type在第 2 行和第 5 行,您可以指定术语类型

Just adding to @Devon Rutledge 's answer, that Scanner class separates tokens (in this case, numbers) on the condition that there are 1 or more spaces between them.只是添加到@Devon Rutledge的答案中,该 Scanner 类在标记(在本例中为数字)之间有 1 个或多个空格的情况下分隔标记。

Hence, as Devon mentioned above, you should add spaces between the inputs.因此,正如上面提到的 Devon,您应该在输入之间添加空格。

However, the answer posted above works the exact same way as a normal sc.next() would, albeit with a problem that if you enter two spaces between inputs, it will five an error.但是,上面发布的答案的工作方式与正常的sc.next()完全相同,尽管存在一个问题,即如果您在输入之间输入两个空格,则会出现五个错误。 So there's no problem with your code, per se, but just the inputs.所以你的代码本身没有问题,但只是输入。

If you do want to accept single-digit numbers without spaces, then you can try this:如果你确实想接受没有空格的个位数,那么你可以试试这个:

Scanner sc = new Scanner(System.in);
sc.useDelimiter("");
int x = sc.nextInt();
int y = sc.nextInt();

Also, it is better to use sc.nextInt() , as sc.next() would accept even the letters, which cannot not be converted to int .此外,最好使用sc.nextInt()sc.next()甚至会接受的字母,这不能不能转换为int

暂无
暂无

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

相关问题 如何使用Java Scanner接受整数输入? NoSuchElementException - How can I use Java Scanner to take an integer input? NoSuchElementException 如何在 Java 中将单行整数作为输入? - How can I take a single line of integers as input in Java? 如何获取用户输入并将其用作整数? - How can I take a user's input and use it as an integer? 如何比较两个Java字符串而不必担心Parse查询中的任何一个的大小写? - How can I compare two Java strings without having to worry about case for either one in Parse queries? 我如何在 java 中使用逗号分隔在单行中获取多个输入 - how can i take multiple input in single line using coma separation in java 如何使用 Java 扫描仪从同一行保存字符串、双精度和 Integer? - How can I save a String, a Double, and an Integer from the same line using Java Scanner? 如何在 Java 中同时检查下一个输入是否为 &gt; 和 &lt; 时检查下一个输入是否为整数? - How can I check that the next input is an integer while checking if it's > and < at the same time in Java? 如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中? - How can I take multiple integer input from scanner and store each integer in separate arrays? 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? 我们如何在java的命令行参数中逐行输入 - how can we take input line by line in command line argument in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM