如何使用 For 循环在 Java 中获取用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27692040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a For loop to get user input in Java?
提问by 01110100
My entire code is posted below. My issue can be seen in this output:
我的整个代码发布在下面。我的问题可以在这个输出中看到:
Enter the character representation of your first configuration: p
How many symbols are under this configuration? 3
Enter the first symbol: Enter the first symbol: 1
Enter the first symbol: None
[, 1, None]
As you can see, the first iteration of the for loop is executed without getting user input. It appears as if it has been skipped.
如您所见,for 循环的第一次迭代是在没有获得用户输入的情况下执行的。它似乎已被跳过。
My question is this: How do I create a For loop that will ask for user input for each iteration of the loop?Not skip an iteration as my code is doing now. I've looked all over the internet for the answer, but I can't find anything specifically.
我的问题是:如何创建 For 循环来要求用户为循环的每次迭代输入输入?不要像我的代码现在那样跳过迭代。我已经在互联网上寻找答案,但我找不到任何具体的东西。
import java.util.Scanner;
import java.util.ArrayList;
class read {
public static void main(String[] args) {
// Create instance of class
Tape instructions = new Tape();
// Assign scanner to keyboard variable
Scanner keyboard = new Scanner(System.in);
// Get user input for mConfig and convert to char
System.out.print("Enter the character representation of your first configuration: ");
String userMConfig = keyboard.nextLine();
char c = userMConfig.charAt(0);
// Get number of symbols/operations/f-configurations
System.out.print("How many symbols are under this configuration? ");
int numOfSymbols = keyboard.nextInt();
// Define array of symbols
ArrayList<String> userSymbols = new ArrayList<String>();
for (int i = 1; i<= numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}
// Assign values to object methods
instructions.mConfig(c);
instructions.symbols(userSymbols);
// Testing code
System.out.println(instructions.getSymbols());
}
}
Here is just my For loop:
这只是我的 For 循环:
for (int i = 1; i<= numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}
采纳答案by dasblinkenlight
When you do this
当你这样做时
int numOfSymbols = keyboard.nextInt();
the scanner takes the integer portion from your input line, but leaves '\n'
in the buffer. That is why when you call
扫描器从您的输入行中获取整数部分,但留'\n'
在缓冲区中。这就是为什么当你打电话
String userInputSymbol = keyboard.nextLine();
from the first iteration of your loop, that '\n'
gets returned immediately, producing an empty line.
从循环的第一次迭代开始,'\n'
立即返回,产生一个空行。
To fix this problem, add keyboard.nextLine();
right after reading numOfSymbols
:
要解决此问题,请keyboard.nextLine();
在阅读后立即添加numOfSymbols
:
int numOfSymbols = keyboard.nextInt();
keyboard.nextLine(); // Throw away the '\n' from the line that contained int
Alternatively, you could use keyboard.nextLine()
followed by parsing an int
manually for reading the integer.
或者,您可以使用keyboard.nextLine()
后跟int
手动解析来读取整数。
In general, you should be careful mixing nextLine
with other calls to Scanner
's methods, precisely because of the issue of trailing '\n'
characters.
通常,您应该小心混合nextLine
其他对Scanner
的方法的调用,正是因为尾随'\n'
字符的问题。
回答by Christian
This happens becasue nextInt()
doesn't consume the end-of-line character.
发生这种情况是因为nextInt()
不消耗行尾字符。
What can you do?You can consume it manually after the nextInt()
call:
你能做什么?您可以在nextInt()
调用后手动使用它:
int numOfSymbols = keyboard.nextInt();
...
keyboard.nextLine(); // Consume the end-of-line character
for (int i = 1; i <= numOfSymbols; i++) {
...
}
You can refer to this postif you want more information.
如果您想了解更多信息,可以参考这篇文章。
回答by Sebi
The problem here is that nextInt()
reads an int, and leaves the newline character in the stream. Then, call to nextLine()
reads this character and return empty string. To avoid that use the following:
这里的问题是nextInt()
读取一个 int,并将换行符留在流中。然后,调用nextLine()
读取该字符并返回空字符串。为避免这种情况,请使用以下方法:
int numOfSymbols = keyboard.nextInt();
keyboard.nextLine(); // this will swallow the "\n"
// Now your for loop should work fine
for (int i = 0; i < numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}