여기서는 사용자로부터 두 개의 키보드 입력을 단일 배열 인덱스 위치로 가져 오려고했습니다.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tour;
import java.util.Scanner;
import tour.City;
/**
*
* @author dp
*/
public class Tour {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
City[] city = new City[9];
Scanner in = new Scanner(System.in);
for(int i=0;i<city.length;i++)
{
int no = in.nextInt();
String name = in.nextLine();
city[i]= new City(no,name);
}
}
}
나는 자바를 처음 접했고 이것을 해결하는 방법을 모릅니다.
때문에 12
그리고 NY
당신이 할 때, 다른 라인에있는
String name = in.nextLine();
String
다시 얻을 비어 있습니다. 이는 Scanner
의 "읽기 지점"이 뒤에 12
있지만 뒤에 오는 줄 끝 마커 앞에 위치 하기 때문입니다.
다른을 추가 nextLine
하고 그 결과를 삭제 하여이 문제를 해결할 수 있습니다 .
in.nextLine(); // Skip to end-of-line after the number
String name = in.nextLine();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다