[英]I want to read those inputs in a single line separated by space in java without using String?
System.out.println("Number of pages + Number of lost pages + Number of Readers");
int n = s.nextInt();
int m = s.nextInt();
int q = s.nextInt();
I want to read input values all the values are going to be integer but I want to read it in a same line with changing it form Integer.我想读取输入值,所有值都将是整数,但我想在同一行中读取它并将其更改为整数。
Assuming s
is an instance of Scanner
: Your code, as written, does exactly what you want.假设s
是Scanner
一个实例:您编写的代码完全符合您的要求。
scanners are created by default with a delimiter configured to be 'any whitespace'.默认情况下,使用配置为“任何空格”的分隔符创建扫描仪。 nextInt()
reads the next token (which are the things in between the delimiter, ie the whitespace), and returns it to you by parsing it into an integer. nextInt()
读取下一个标记(即分隔符之间的内容,即空格),并通过将其解析为整数将其返回给您。
Thus, your code as pasted works fine .因此,您粘贴的代码工作正常。
If it doesn't, stop setting up a delimiter, or reset it back to 'any whitespace' with eg scanner.reset();
如果没有,请停止设置分隔符,或使用例如scanner.reset();
将其重置回“任何空白” scanner.reset();
or scanner.useDelimiter("\\\\s+");
或scanner.useDelimiter("\\\\s+");
. .
class Example {
public static void main(String[] args) {
var in = new Scanner(System.in);
System.out.println("Enter something:");
System.out.println(in.nextInt());
System.out.println(in.nextInt());
System.out.println(in.nextInt());
}
}
works fine here.在这里工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.