[英]how to declare inputs on one line in java
I'm having trouble making my program read in all of the integers on one line rather than having to enter each number and hit enter after each digit in the terminal window. 我无法让我的程序在一行上读取所有整数,而不必在终端窗口中输入每个数字并在每个数字后按回车。
For example, the terminal window would read: 例如,终端窗口将显示为:
For the our text enter the first 9 digits: 013292373"
rather than 而不是
For the our text enter the first 9 digits: 0
1
2
3
... etc"
My code looks like this so far: 到目前为止,我的代码如下:
/**
* This program calculates the last number of a 10 digit ISBN number.
*/
import java.util.Scanner;
public class ISBNnum
{
public static void main(String[] args)
{
//Variables and Scanner
Scanner input = new Scanner(System.in);
int dOne;
int dTwo;
int dThree;
int dFour;
int dFive;
int dSix;
int dSeven;
int dEight;
int dNine;
int checksum;
//Input
System.out.print("For the our text enter the first 9 digits: ");
dOne = input.nextInt();
dTwo = input.nextInt();
dThree = input.nextInt();
dFour = input.nextInt();
dFive = input.nextInt();
dSix = input.nextInt();
dSeven = input.nextInt();
dEight = input.nextInt();
dNine = input.nextInt();
//Calculation
checksum = ((dOne * 1) + (dTwo * 2) + (dThree * 3) + (dFour * 4) + (dFive * 5) + (dSix * 6) +
(dSeven * 7) + (dEight * 8) + (dNine * 9)) % 11;
//Output
if (checksum == 10)
{
System.out.print("The whole ISBN is "+dOne+dTwo+dThree+dFour+dFive+dSix+dSeven+dEight+
dNine+"X");
}
else if (checksum < 10)
{
System.out.println("The whole ISBN is " + dOne + dTwo + dThree + dFour + dFive + dSix +
dSeven + dEight + dNine + " - " + checksum);
}
}
}
Thanks for your help guys! 感谢您的帮助!
Scanner s = new Scanner(System.in).useDelimiter("\\s*");
Adapted from : 改编自 :
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
Try using a scanner and splitting the String
into a String[]
by the spaces 尝试使用扫描仪,并按空格将
String
拆分为String[]
import java.util.Scanner;
public class Foo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in)
String input = s.nextLine();
String[] parts = input.split(" ");
for (String part : parts) {
System.out.println(part);
}
}
}
I extended my above answer with the below code. 我用下面的代码扩展了上面的答案。 It worked when I compiled and ran it, separating nine numbers by the spaces.
当我编译并运行它时,它起作用了,用空格分隔了九个数字。
public static void main(String[] args) {
//Variables and Scanner
Scanner input = new Scanner(System.in);
int[] numbers = new int[9];
int checksum = 0;
//Input
System.out.print("For the our text enter the first 9 digits, separated by spaces: ");
String numInput = input.nextLine();
String[] parts = numInput.split(" ");
for (int i = 0; i < parts.length; i++) {
numbers[i] = Integer.parseInt(parts[i]);
checksum += numbers[i] * (i + 1);
}
//Calculation
checksum %= 11;
}
Tracing through the code, first I create a Scanner input
object, an int[] numbers
with 9 indices, and an int checksum
instantiated at zero. 跟踪代码,首先创建一个
Scanner input
对象,一个带有9个索引的int[] numbers
,以及一个实例化为零的int checksum
。 Next, I prompt the user to input nine numbers, separated by spaces, then receive the entire line with input.nextLine()
, which returns the stdin
up to a newline character. 接下来,我提示用户输入用空格分隔的九个数字,然后使用
input.nextLine()
接收整行,这会将stdin
返回到换行符。 I then split the numInput
into a String[] parts
by the spaces, making everything up the first space index zero, second space is index one, etc.. I iterate through the list, changing each String
representation of the numbers into int
objects and place it in the numbers
array at index i
. 然后,我将
numInput
按空格拆分为String[] parts
,使所有内容均在第一个空格处索引为零,在第二个空格处索引为1, numInput
。我遍历列表,将数字的每个String
表示形式更改为int
对象,然后将其放在索引i
的numbers
数组中。 I then update the checksum
Integer
with numbers[i]
(the number just converted) and multiply by i+1
. 然后,我用
numbers[i]
(刚刚转换的数字)更新checksum
Integer
并乘以i+1
。 After the loop, I modulus the checksum
by 11. 循环后,我将
checksum
乘以11。
The input on the terminal will look like 终端上的输入看起来像
For the our text enter the first 9 digits, separated by spaces: 1 4 6 2 8 7 2 4 10
If you wanted to, change the line where it says String[] parts = input.split(" ");
如果需要,更改显示
String[] parts = input.split(" ");
to String[] parts = input.split(",");
到
String[] parts = input.split(",");
and you could input your numbers like so: 您可以这样输入数字:
1,4,6,2,8,7,2,4,10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.