在Java中,如何在for循环中接受不同数据类型的多个用户输入?
我试图提示用户输入一个字符串,该字符串将存储在字符串数组中,然后输入一个int,该int将被放入int数组中 我遇到了一个问题,第一行是打印的,但是没有提示用户输入字符串。然后立即打印第二条print语句,用户只能键入int 到目前为止,我已经:在Java中,如何在for循环中接受不同数据类型的多个用户输入?,java,for-loop,java.util.scanner,user-input,Java,For Loop,Java.util.scanner,User Input,我试图提示用户输入一个字符串,该字符串将存储在字符串数组中,然后输入一个int,该int将被放入int数组中 我遇到了一个问题,第一行是打印的,但是没有提示用户输入字符串。然后立即打印第二条print语句,用户只能键入int 到目前为止,我已经: int i, n = 10; String[] sentence = new String[1000]; int[] numbers = new int[1000]; for(i = 0; i < n; i+
int i, n = 10;
String[] sentence = new String[1000];
int[] numbers = new int[1000];
for(i = 0; i < n; i++)
{
System.out.println("Enter String" + (i + 1) + ":");
sentence[i] = scan.nextLine();
System.out.printf("Enter int " + (i + 1) + ":");
numbers[i] = scan.nextInt();
}
在这里,您可以输入一个int,并将其存储到int数组中。但是不能为字符串数组输入字符串
请提供帮助。像这样放置scan.nextLine():
for(i = 0; i < n; i++){
System.out.println("Enter String" + (i + 1) + ":");
sentence[i] = scan.nextLine();
System.out.printf("Enter int " + (i + 1) + ":");
numbers[i] = scan.nextInt();
scan.nextLine();
}
(i=0;i{
System.out.println(“输入字符串”+(i+1)+“:”;
句子[i]=scan.nextLine();
System.out.printf(“输入int”+(i+1)+“:”;
数字[i]=scan.nextInt();
scan.nextLine();
}
像这样放置scan.nextLine():
for(i = 0; i < n; i++){
System.out.println("Enter String" + (i + 1) + ":");
sentence[i] = scan.nextLine();
System.out.printf("Enter int " + (i + 1) + ":");
numbers[i] = scan.nextInt();
scan.nextLine();
}
(i=0;i{
System.out.println(“输入字符串”+(i+1)+“:”;
句子[i]=scan.nextLine();
System.out.printf(“输入int”+(i+1)+“:”;
数字[i]=scan.nextInt();
scan.nextLine();
}
此问题是由nextInt()方法引起的
这里发生的是nextInt()
方法使用用户输入的整数,而不是用户输入末尾的新行字符,该字符是在按enter键时创建的
因此,当输入整数后按enter键时,对nextLine()
的下一次调用将使用新的行字符,该字符在nextInt()方法上次循环中没有使用。这就是为什么它在循环的下一次迭代中跳过String
的输入,而不等待用户输入String
解决方案
您可以通过在nextInt()
调用之后调用nextLine()
来使用新行字符
for(i = 0; i < n; i++)
{
System.out.println("Enter String" + (i + 1) + ":");
sentence[i] = scan.nextLine();
System.out.printf("Enter int " + (i + 1) + ":");
numbers[i] = scan.nextInt();
scan.nextLine(); // <------ this call will consume the new line character
}
(i=0;i
{
System.out.println(“输入字符串”+(i+1)+“:”;
句子[i]=scan.nextLine();
System.out.printf(“输入int”+(i+1)+“:”;
数字[i]=scan.nextInt();
scan.nextLine();//此问题是由nextInt()方法引起的
这里发生的是nextInt()
方法使用用户输入的整数,而不是用户输入末尾的新行字符,该字符是在按enter键时创建的
因此,在输入整数后按enter键时,对nextLine()的下一次调用将使用新的行字符,而nextInt()在循环的最后一次迭代中没有使用该字符
方法。这就是为什么它在循环的下一次迭代中跳过字符串的输入,而不等待用户输入字符串的原因
解决方案
您可以通过在nextInt()
调用之后调用nextLine()
来使用新行字符
for(i = 0; i < n; i++)
{
System.out.println("Enter String" + (i + 1) + ":");
sentence[i] = scan.nextLine();
System.out.printf("Enter int " + (i + 1) + ":");
numbers[i] = scan.nextInt();
scan.nextLine(); // <------ this call will consume the new line character
}
(i=0;i
{
System.out.println(“输入字符串”+(i+1)+“:”;
句子[i]=scan.nextLine();
System.out.printf(“输入int”+(i+1)+“:”;
数字[i]=scan.nextInt();
scan.nextLine();//可能重复的scan.nextLine()的可能重复;显然需要在第一个print语句之后和句子[i]之前语句,以便读取第一个实例。非常感谢!@Anon如果您将它放在for循环的末尾或第一个System.out.println
语句之后,它将起作用。只需知道您需要在nextInt()方法调用之后使用新行字符。scan.nextLine();显然需要在第一个打印语句之后和句子之前[i]语句,以便读取第一个实例。非常感谢!@Anon如果您将它放在for循环的末尾或第一个System.out.println
语句之后,它将起作用,只需知道您需要在nextInt()方法调用之后使用新行字符即可。