I am having a problem completing my program. I am sure it is a simple mistake.

My results look something like this

How many students do you have?
2
How many Exam grades do you have?
4
Enter a score for student number: 1
98
Enter a score for student number: 1
78
Enter a score for student number: 1
88
Enter a score for student number: 1
83
Enter a score for student number: 1

77
Enter a score for student number: 1
77
Enter a score for student number: 1
77
Enter a score for student number: 1

import java.util.Scanner;
public class GradesAverage { 
    private static Scanner testScan;

    //Program is not distinguishing between the students so when I run the program it never stops asking for the grades for student 1

    public static void main(String[] args) {

    int studTotal = 0;
    int numScores = 0;
    int lowest = 0;
    int students = 0;
    double studNormAve = 0;
    testScan = new Scanner(System.in);
    System.out.println("How many students do you have?"); 
    students = testScan.nextInt();
    System.out.println("How many Exam grades do you have?");
    numScores = testScan.nextInt();
    int[] studentsArray = new int[students];    
    int[] scoresArray = new int[numScores];
    for(int bigTemp: studentsArray){ //determines how many scores are entered for each student      
    for(int temp: scoresArray) //determines the total and average scores for each student
    { 

            int n = bigTemp+1;
            System.out.println("Enter a score for student number: "+n);
            temp = testScan.nextInt();
            studTotal += temp;

                if(temp<lowest)
                {
                lowest = temp;

                 }

                }
    }           
                studNormAve = (double)(studTotal/numScores);
                System.out.println("Here is the student's average test scores.");
                System.out.println(studNormAve);
                {

                if ((studNormAve>= 90) && (studNormAve <= 100)){
                    System.out.println("The student's grade is A. ");}
                else if ((studNormAve >= 80) && (studNormAve < 90)){
                    System.out.println("The letter grade is B");} 
                else if ((studNormAve >= 70) && (studNormAve < 80)){
                    System.out.println("The letter grade is C");} 
                else if ((studNormAve >= 60) && (studNormAve < 70)){
                    System.out.println("The letter grade is D");} 
                else if ((studNormAve >= 0) && (studNormAve < 60)){
                    System.out.println("The letter grade is F");} 
                }
    }
}

Recommended Answers

All 4 Replies

Please review the following documentation:

For-each loop

"...When you see the colon (:) read it as “in.” "

Applied to one of your loops:
for(int bigTemp: studentsArray){

The loop reads as “for each int bigTemp in studentArray.”

Arrays

"a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value"

Initial Values of Variables

"For type int, the default value is zero, that is, 0."

For loop

"...the variable...holds the current value from the...array..."

Applied to one of your loops:
for(int bigTemp: studentsArray){

Assume we entered "3" for students.

Then,

students = 3

The code states:

int[] studentsArray = new int[students];

So,

int[] studentsArray = new int[3]; 

On the first iteration,
bigTemp = studentsArray[0] which contains 0.

On the second iteration,
bigTemp = studentsArray[1] which contains 0.

On the third iteration,
bigTemp = studentsArray[2] which contains 0.

So, as you can see the value "n" you have in line 25:

int n = bigTemp+1;

is always 1.

students: 2
exam grades: 4

students x exam grades = 2 x 4 = 8

which is the number of times the following occurs:

System.out.println("Enter a score for student number: "+n);

Hint: Use the following for loops:

If want to print out the average score and letter grade for each student before prompting for the scores from the next student, you don't need an array.

double[] studentsArray = new double[students];   

for(int i= 0; i < students; i++){ 

    for (int j=0; j < numScores; j++){

        //ToDo: get scores here

    }//for

    //calculate average
    studNormAve = (double)(studTotal/numScores);

    //ToDo: print out averages and grade letter


 }//for

Otherwise, put the values in the array and print out the averages and letter grade later.

    double[] studentsArray = new double[students];   

    for(int i= 0; i < students; i++){ 

        for (int j=0; j < numScores; j++){

            //ToDo: get scores here

        }//for

        //calculate average
        studNormAve = (double)(studTotal/numScores);

        //put average into array
        studentsArray[i] = studNormAve;

     }//for


     for (double studentAverage : studentsArray)
     {
         //put value from array into
         //studNormAve
         studNormAve = studentAverage;


         //ToDo: print out averages and grade letter

     }
public class Grading {
  public static void main(String... args) {
    System.out.println("you fail!");
  }
}

That's pretty much the standard grade for the typical homework question...

In your case, right off the bat you fail to set "bigTemp" to the right number.
Stopped looking there.

Thank you, cgeier. I see what I was doing now! I really appreciate the explanations. It helps me understand much better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.