2016-08-07 1 views
1

약간의 질문이 있습니다. 이름과 성을 입력 할 때 한 줄에 두 단어를 읽어야합니다. 나는,이 결과 (내가 줄에 두 개 이상의 단어를 사용하는 것을 알고 x[y]=Marbis.nextLine();
,하지만 두 번째 기회에이 같은 나에게 오류를 표시합니다 : 여기2 차원 배열, 스레드 "main"의 예외 java.util.InputMismatchException

public void Promedios5(){ 
    Scanner Marbis=new Scanner(System.in); 
    String[] x=new String[5]; 
    double[][] a=new double[5][4]; 
    double[] b=new double [5],c=new double[5]; 
    System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n" 
      + "se le solicitarán a usted, el nombre y las cuatro notas"); 
    System.out.println("Podría ingresarlas ahora por favor:"); 
    for(int y=0;y<=4;y++){ 
     System.out.println("Ingrese el nombre:"); 
     x[y]=Marbis.nextLine(); 
     for(int z=0;z<=3;z++){ 
      a[y][z]=Marbis.nextDouble(); 
     } 
     b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3]; 
     c[y]=b[y]/4; 
    } 
    System.out.println("Ahora usted verá los promedios de las personas:"); 
    System.out.println("Nombre:\t\t\tPromedio"); 
    for(int m=0;m<=4;m++) 
     System.out.printf("%s:\t\t%.2f\n",x[m],c[m]); 
} 

나는 오류가 내가) 꽵에 배열을 사용할 수 있다고 생각 : 자바에서

MArio Albert 
100 
100.00 
78.00 
100.00 
Ingrese el nombre: 
John Antoinie 
Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Scanner.java:864) 
    at java.util.Scanner.next(Scanner.java:1485) 
    at java.util.Scanner.nextDouble(Scanner.java:2413) 
    at vectormarbis1.MarbisVectors2.Promedios5(MarbisVectors2.java:125) 
    at vectormarbis1.VectorMarbis1.main(VectorMarbis1.java:28) 
C:\Users\ManoloAurelio\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 39 seconds) 
+0

이중 값을 취한 후에는 사용자의'x [y] = Marbis.nextLine();'이 줄 바꿈 문자를 문자열로 사용하기 때문입니다. 이중 값을 취한 후 입력을 플러시하십시오. – VatsalSura

답변

0

에만 flush 출력.

나머지 줄을 버릴 때가 있습니까? 문제를 해결하기 위해 당신은 당신이 다음 줄에서 읽을 수 예상대로 nextDouble() 후이 작업을 수행 할 필요가

input.nextLine(); 

호출 할 수 있습니다.

아래 주어진 코드가 문제를 해결하는 데 도움이되기를 바랍니다.

public void Promedios5(){ 
    Scanner Marbis=new Scanner(System.in); 
    String[] x=new String[5]; 
    double[][] a=new double[5][4]; 
    double[] b=new double [5],c=new double[5]; 
    System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n" 
    + "se le solicitarán a usted, el nombre y las cuatro notas"); 
    System.out.println("Podría ingresarlas ahora por favor:"); 
    for(int y=0;y<=4;y++){ 
    System.out.println("Ingrese el nombre:"); 
    x[y]=Marbis.nextLine(); 
    for(int z=0;z<=3;z++){ 
     a[y][z]=Marbis.nextDouble(); 
    } 
    Marbis.nextLine(); //Just add this line here 
    b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3]; 
    c[y]=b[y]/4; 
    } 
    System.out.println("Ahora usted verá los promedios de las personas:"); 
    System.out.println("Nombre:\t\t\tPromedio"); 
    for(int m=0;m<=4;m++) 
    System.out.printf("%s:\t\t%.2f\n",x[m],c[m]); 
}