2012-11-29 2 views
-5

여기서 코드이다누군가 내 Java 코드를 해결할 수 있습니까? 나는 두 번째 이름에 대한 입력을 할 수 없다. 내가해야 할 일은 무엇인가?

import java.util.Scanner; 
public class Array2 
{ 
    static Scanner input = new Scanner (System.in); 

    public static void main (String [] args) 
    { 
    String [] studentNames = new String [5]; 
    student_Names (studentNames); 
    } 

    public static void student_Names (String [] studentNames) 
    { 
    for (int x = 0; x < studentNames.length; x++) 
    { 
     System.out.printf("\nStudent %d name : ", x+1); 
     studentNames[x] = input.nextLine(); 
     student_Scores(); 
    } 
    } 

    public static void student_Scores() 
    { 
    double [] studentScores = new double [4]; 
    int x; 
    for (x = 0; x < studentScores.length; x++) 
    { 
     System.out.printf("\nEnter marks for test %d: ",x+1); 
     studentScores[x] = input.nextDouble(); 
    } 
    average_Scores(studentScores); 
    } 

    public static void average_Scores(double [] a) 
    { 
    int i; 
    double sum, average; 
    for (i = 0, sum = 0; i < a.length; i++) 
    { 
     System.out.printf("Marks Test %d : %.2f\n", i+1, a[i]); 
    } 

    for (i = 0, sum = 0; i < a.length; i++) 
    { 
     sum = sum + a[i]; 
    } 
    average = sum/a.length; 
    student_Grades (average); 
    System.out.printf("\nAverage Score : %.2f", average); 
    } 

    public static void student_Grades (double b) 
    { 
    String c = ""; 
    if (b > 0 && b < 60) 
     c = "F"; 
    if (b >= 60 && b < 70) 
     c = "D"; 
    if (b >= 70 && b < 80) 
     c = "C"; 
    if (b >= 80 && b < 90) 
     c = "B"; 
    if (b >= 90 && b <= 100) 
     c = "A"; 
    System.out.printf("\nGrade   : %s", c); 
    } 
} 

그리고이 수행 될 때의 출력이다

Student 1 name : sss 

Enter marks for test 1: 12 

Enter marks for test 2: 23 

Enter marks for test 3: 34 

Enter marks for test 4: 45 
Marks Test 1 : 12.00 
Marks Test 2 : 23.00 
Marks Test 3 : 34.00 
Marks Test 4 : 45.00 

Grade   : F 
Average Score : 28.50 
Student 2 name : 
Enter marks for test 1: 
+5

보고있는 오류의 관점에서이 질문을 다시 시도하십시오. –

+2

그의 프로그램은 기본적으로 5 명의 학생 이름과 4 개의 시험 성적 정보를 요구합니다. 그가 가지고있는 문제는 프로그램이 2 학년, 3 학년, 4 학년 및 5 학년 학생의 이름 만 묻는 것이 아니라 자신의 점수 만 묻는 것입니다. –

답변

0

문제가 student_Names 방법에서 찾아 볼 수있다; 대신 :

if(x>0) input.nextLine(); 
studentNames[x] = input.nextLine(); 

문제가 자바에서 오는

studentNames[x] = input.nextLine(); 

사용하면 사용자가 이미 다음 이름에 대한 입력을 주신 믿음 mistakingly; input.nextLine();은 기본적으로이 입력을 가져 와서 버리고 자연 상태로 되돌립니다.

그러나 첫 번째 학생의 이름 입력이 올바로 작동하기 때문에 나머지 학생 이름에만 적용하려면 if(x>0) 문이 필요합니다.

+0

감사합니다. 사용자 이름 tbd. 지금은 이해. –

+0

내가 물어보고 싶은 질문이 하나 더 있는데, 사용자가 0보다 작은 점수를 입력하거나 100을 초과하여 시도한 후 catch를 사용하면 오류 메시지가 어떻게 표시 될 수 있습니까? –

관련 문제