如何在java中一行声明输入
我很难让我的程序读取一行中的所有整数,而不是必须输入每个数字,然后在终端窗口中的每个数字后按enter键 例如,终端窗口将显示:如何在java中一行声明输入,java,input,Java,Input,我很难让我的程序读取一行中的所有整数,而不是必须输入每个数字,然后在终端窗口中的每个数字后按enter键 例如,终端窗口将显示: For the our text enter the first 9 digits: 013292373" 而不是 For the our text enter the first 9 digits: 0 1 2 3 ... etc" 到目前为止,我的代码如下所示: /** * This program calculates the last number of
For the our text enter the first 9 digits: 013292373"
而不是
For the our text enter the first 9 digits: 0
1
2
3
... etc"
到目前为止,我的代码如下所示:
/**
* 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);
}
}
}
/**
*此程序计算10位ISBN数字的最后一个数字。
*/
导入java.util.Scanner;
公共类ISBNnum
{
公共静态void main(字符串[]args)
{
//变量和扫描器
扫描仪输入=新扫描仪(System.in);
int完成;
int-dTwo;
int-dThree;
int-dFour;
int-dFive;
int dSix;
int-ds偶;
油漆灯;
int dNine;
整数校验和;
//输入
System.out.print(“对于我们的文本,输入前9位:”);
dOne=input.nextInt();
dTwo=input.nextInt();
dThree=input.nextInt();
dFour=input.nextInt();
dFive=input.nextInt();
dSix=input.nextInt();
ds偶数=input.nextInt();
dEight=input.nextInt();
dNine=input.nextInt();
//算计
校验和=((完成*1)+(数据两个*2)+(数据三个*3)+(数据四个*4)+(数据五个*5)+(数据六个*6)+
(DS偶*7)+(dEight*8)+(dNine*9))%11;
//输出
如果(校验和=10)
{
System.out.print(“整个ISBN是”+dOne+dTwo+dThree+dFour+dFive+dSix+ds偶数+dEight+
dNine+“X”);
}
否则如果(校验和<10)
{
System.out.println(“整个ISBN是”+dOne+dTwo+dThree+dFour+dFive+dSix+
DS偶数+dEight+dNine+“-”+校验和);
}
}
}
谢谢你们的帮助,伙计们
Scanner s = new Scanner(System.in).useDelimiter("\\s*");
改编自:尝试使用扫描仪,按空格将
字符串拆分为字符串[]
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);
}
}
}
我用下面的代码扩展了我的上述答案。当我编译并运行它时,它工作了,用空格分隔九个数字
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;
}
publicstaticvoidmain(字符串[]args){
//变量和扫描器
扫描仪输入=新扫描仪(System.in);
整数[]个数=新整数[9];
整数校验和=0;
//输入
System.out.print(“对于我们的文本,输入前9位数字,用空格分隔:”);
字符串numInput=input.nextLine();
String[]parts=numInput.split(“”);
对于(int i=0;i
通过跟踪代码,首先我创建一个Scanner input
对象、一个带有9个索引的int[/code>和一个实例化为零的int校验和。接下来,我提示用户输入九个数字,以空格分隔,然后使用input.nextLine()
接收整行,它将stdin
返回到换行符。然后我将numInput
按空格分割成String[]parts
,使第一个空格索引为零,第二个空格索引为一,以此类推。。我遍历列表,将数字的每个字符串
表示形式更改为int
对象,并将其放置在索引I
处的numbers
数组中。然后,我用数字[I]
(刚刚转换的数字)更新校验和整数
,并乘以I+1
。在循环之后,我将校验和
模11
终端上的输入看起来像
对于我们的文本,输入前9位数字,用空格分隔:1 4 6 2 8 7 2 4 10
如果需要,请更改显示String[]parts=input.split(“”)的行
toString[]parts=input.split(“,”)
您可以这样输入您的数字:
1,4,6,2,8,7,2,4,10
看看这里:作为这方面的新手,我很难理解我到底需要在哪里使用什么。我还没有学会在这段代码中输入与我相关的内容。您是否介意提供一些澄清,说明我需要做些什么来操作这些指令以便为我工作