【发布时间】:2014-04-06 14:50:31
【问题描述】:
int number = keyboard.nextInt();
String [][] myArray = new String[number][1]
for (int i = 0; i < x; i++)
{
System.out.println("Enter food name: ");
myArray[i][0] = keyboard.nextLine();
for (int j = 0; j < 1; j++)
{
System.out.println("Enter the type of this food: ");
myArray[i][j] = keyboard.nextLine();
}
}
这是我要问的代码。我希望它在我运行这个程序时打印出这些输出:
Enter food name:
(where user type his or her input)
Enter the type of this food:
(where user type his or her input)
(My problem is it prints out this instead for the first loop:)
Enter food name:
Enter the type of this food:
(where user type his or her input)
无法进入食物项目。在第一个循环之后,程序恢复正常并显示我想要的输出,但是如何更改代码以便第一个循环将接受用户输入“输入食物名称:”?
提前致谢。
编辑:很抱歉没有事先看到这个问题,但这个问题与 for 循环无关,所以如果我在下一个输入之前输入一个空字符串,我的循环将无法工作。有什么解决办法吗?
编辑:让它工作。谢谢大家。
【问题讨论】:
-
抱歉没有早点看到。有没有解决循环的方法?和那个问题的答案一样吗?
-
只需在
int number = keyboard.nextInt();行后面写上keyboard.nextLine();即可。 -
好的,我会试试的。谢谢
-
再次感谢 Aman Agnihotri。它确实有效xD。如果你愿意,你可以把它作为这个问题的答案,我会选择它作为最好的。
标签: java arrays user-input