Hey Guys,
I am a new student to Java and I am working on an assignment for school. I am not looking for anyone to do my work for me, just some guidance on what I can do to fix the problem. I have done research on this issue on google and visited various sites without getting the information I am looking for. I have contacted my professor as well, but he is usually very slow at responding to my questions. I am running out of time for this assignment and I would like to fix this bug before submitting the assignment.
Okay, now that the formalities are out of the way, here is the code that I have written thus far:
import java.util.Scanner;
public class CalculateGrade {
public static void main(String[] args) {
//main method calls AssignvalueToArray method, and AssignValueToArray method calls
//DisplayvalueInArray method
//create a Scanner object for input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int studentNum = keyboard.nextInt();
System.out.println();
System.out.print("Enter the number of Exams: ");
int studentExamNo = keyboard.nextInt();
AssignValueToArray(studentNum, studentExamNo);
}
//This method assigns students' scores to a two dimensional array
public static void AssignValueToArray(int studentNumber, int numberOfExam)
{
int[][] studentScore = new int[studentNumber][numberOfExam];
Scanner keyboardForArray = new Scanner(System.in);
int studCount = 1;
for (int index = 0; index < studentNumber; index++)
{
System.out.println ("Please type in student " + studCount + " 's grades: " );
for(int indexOfExam=0; indexOfExam < numberOfExam; indexOfExam++)
{
studentScore[index][indexOfExam] = keyboardForArray.nextInt();
studCount++;
}
DisplayvalueInArray(studentScore);
}
}
//this method displays student score in the two dimensional array
public static void DisplayvalueInArray(int[][] studentScoreArray)
{
System.out.println ("The students' scores are: " + "\n");
int studentCount = 1;
for (int index = 0; index < studentScoreArray.length; index++)
{
System.out.print("Grades for student " + studentCount +": ");
for (int indexOfExam = 0; indexOfExam < studentScoreArray[index].length;
indexOfExam++)
{
char letterGrade;
if (studentScoreArray[index][indexOfExam] <= 59)
{
letterGrade = 'F';
}
else if (studentScoreArray[index][indexOfExam] >= 60 && studentScoreArray[index][indexOfExam] <=69)
{
letterGrade = 'D';
}
else if (studentScoreArray[index][indexOfExam] >= 70 && studentScoreArray[index][indexOfExam] <=79)
{
letterGrade = 'C';
}
else if (studentScoreArray[index][indexOfExam] >= 80 && studentScoreArray[index][indexOfExam] <= 89)
{
letterGrade = 'B';
}
else
letterGrade = 'A';
System.out.print(studentScoreArray[index][indexOfExam]+"\t");
System.out.print(letterGrade);
}
System.out.println();
studentCount++;
}
}
}
The purpose of this program is to take the number of students and number of exams from the user, as well as the number grades, and then output the students grade and letter grade when the user has finished inputting the data. The problem I am running into is when I input more than one students grades I get this as the output:
Enter the number of students: 2
Enter the number of Exams: 2
Please type in student 1 's grades:
88
75
The students' scores are:
Grades for student 1: 88 B75 C
Grades for student 2: 0 F0 F
Please type in student 3 's grades:
After I insert student three's grades I get this output:
Please type in student 3 's grades:
66
88
The students' scores are:
Grades for student 1: 88 B75 C
Grades for student 2: 66 D88 B
So, it skips the third students grades, but outputs the grades as student two's. I am not sure if this bug is being caused within the array or when the program gets the letter grade from the inputted number grade. Any help you guys can give me on this problem would be much appreciated and thank you all in advance.