Hello, I'm Mirza and I'm in the first semester of an IT university. I like to experiment with Java, and occasionally I run into problems. Now, I've been visiting daniweb for quite a while now, and I've always found solutions to my problem, but this particular problem I just can't seem to solve/find the solution for.
The goal:
Write a program where the user can specify how many subjects a student has.
Input names of subjects.
Input grades ofsubjects.
Output subject - grade list.
Output average score.
The problem:
The 18th line gets skipped while the rest of the code gets executed correctly.
The code:
package fakultet;
import java.util.Scanner;
public class Fakultet {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Student theStudent = new Student();
System.out.println("Input number of subjects: ");
theStudent.subjectNum = input.nextInt();
theStudent.grades = new int[theStudent.subjectNum];
theStudent.subjectNames = new String[theStudent.subjectNum];
for (int i=0; i<theStudent.grades.length; i++){
System.out.println("Input name for subject "+i+":");
theStudent.subjectNames[i] = input.nextLine();
System.out.println("Input grade for subject "+i+":");
theStudent.grades[i] = input.nextInt();
}
for (int i=0; i<theStudent.grades.length; i++){
System.out.println(theStudent.subjectNames[i]+" - "+theStudent.grades[i]);
}
System.out.println("Average score: "+avgScore(theStudent.grades));
}
public static int avgScore(int[] gradeList){
int score = 0;
for (int i=0; i<gradeList.length; i++){
score = score + gradeList[i];
}
score = score / gradeList.length;
return score;
}
}
I've also made an object:
package fakultet;
public class Student {
int subjectNum;
String[] subjectNames;
int[] grades;
}