내 전체 코드는 아래에 게시됩니다. 내 문제는이 출력에서 볼 수 있습니다 :For 루프를 사용하여 Java에서 사용자 입력을받는 방법?
위에서 볼 수 있듯이 for 루프의 첫 번째 반복은 사용자 입력없이 실행됩니다. 마치 건너 뛴 것처럼 보입니다.
내 질문은 루프의 각 반복마다 사용자 입력을 요청하는 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());
}
}
는 단지 내 For 루프 :이
int numOfSymbols = keyboard.nextInt();
을 수행 할 때 스캐너가 사용자의 입력 라인의 정수 부분이 필요하지만 버퍼에 '\n'
잎
for (int i = 1; i<= numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}