이 코드는 올바르게 실행되지 않습니다. 두 번째로 루프를 실행하는 이유를 알 수 없습니다. firstName 변수에 대한 입력을받지 않고 "Enter First Name :"만 인쇄합니다.
import java.util.Scanner;
public class Test {
public static void main(String[]args){
String[] firstName = new String[30] ;
String[] secondName = new String [30];
int[] marksCourseWorkOne = new int[30];
int i;
Scanner sc = new Scanner(System.in);
for (i = 28; i < firstName.length; i++){
System.out.print("\nEnter First Name : ");
firstName[i] = sc.nextLine();
System.out.print("\nEnter Second Name : ");
secondName[i] = sc.nextLine();
System.out.print("\nEnter Marks For Course Work One : ");
marksCourseWorkOne[i] = sc.nextInt();
}
}
}
우선 nextLine
after 는 사용하지 마십시오 nextInt
. 후자는 \n
문자를 소비하지 않고 에서 삼켜 서 nextLine
삽입하려는 실제 입력을 건너 뛰게됩니다.
둘째, 당신이 컴파일러라면 무엇이라고 생각 i
하십니까? 프로그래머가 당신에게 말하지 않았기 때문에 당신은 모를 것입니다. 를 작성해야 int i = 28
(당신은 28가 아니라 0해야 확실?).
셋째, 프로그램에 대한 진입 점이 없습니다.이 모든 것이 main
메서드 (또는 다른 메서드) 내에 있어야합니다 .
해결책:
i
.nextLine
후 바로 nextInt
을 소비하는가 \n
.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다