0

I am currently working on a program that takes a user input of the number of students in a class, then, (in a while loop), takes a user input of a student number and their average grade, then, after calculation, prints the highest mark, lowest mark, and average mark of the class.

This is what I have done so far:

import java.util.Scanner;
public class ClassMarks {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of students in class: ");
        int students = input.nextInt();
        int x = students;
        while (x > 0) {
            System.out.println("Enter student number: ");
            double studentNumber = input.nextDouble();
            System.out.println("Enter student grade: ");
            double studentGrade = input.nextDouble();
            x = x - 1;
        }
    }
}

I am looking for a way to get the program to create a new variable for me that stores each new user input student grade inside the while loop. ex) studentGrade1, studentGrade2, studentGrade3 ...

4
  • 1
    Store them in a List. Commented Feb 6, 2016 at 22:57
  • 1
    List is not necessary for the specified requirements. Commented Feb 6, 2016 at 22:59
  • @radoh +1 actually, storing is not necessary. The OP can also keep the highest mark, the lowest mark and the sum of the marks. Commented Feb 6, 2016 at 23:04
  • @ArnaudDenoyelle yup, I've just added such answer/suggestion Commented Feb 6, 2016 at 23:07

2 Answers 2

5

Before the while loop, create variables highest/lowest grades

double highestGrade = Double.MIN_VALUE, lowestGrade = Double.MAX_VALUE;
double gradeSum = 0; 

Then as you loop through the values, adjust the variables appropriately, e.g.

if (studentGrade > highestGrade)
    highestGrade = studentGrade;
if (studentGrade < lowestGrade)
    lowestGrade = studentGrade;
gradeSum += studentGrade;

And then after the loop finishes, get the average like this

double averageGrade = gradeSum / students;
Sign up to request clarification or add additional context in comments.

3 Comments

highestGrade = Math.max(studentGrade, highestGrade) would be nicer :) Same for lowestGrade.
Nicer, true, but it would mean rewriting the variable on every loop, even when the value doesn't change. Hmmm, dilemma... :)
Ohhhhh... You're right, a list isn't required. Thank you!
0

How about using a List.

List<Student> students = new ArrayList<>()

and in while loop

create a student update its fields then add it to the list

Student myNewStudent = new Student();
// update fields 

students.add(myNewStudent);

To iterate the list you can use a for loop.

for (Student s : students) { 
    // Get student info
}

Adapting this into your code:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ClassMarks {
// Create an inner class Student

    public static class Student {

        public double studentNumber;
        public double studentGrade;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of students in class: ");
        int students = input.nextInt();
        int x = students;

        // Create a list to hold your students
        List<Student> studentsList = new ArrayList<>();

        while (x > 0) {
            Student myNewStudent = new Student();
            System.out.println("Enter student number: ");
            myNewStudent.studentNumber = input.nextDouble();
            System.out.println("Enter student grade: ");
            myNewStudent.studentGrade = input.nextDouble();

            // update fields 
            studentsList.add(myNewStudent);

            x = x - 1;
        }

        // Loop your student List
        for (Student s : studentsList) {
            // Get student info
            System.out.println("Student number is: "+s.studentNumber+" grade is : "+s.studentGrade);
        }
    }
}

3 Comments

How would I then refer back to the variables when I need to do the calculations for average grade for example?
all the students will be in "students" list. you can refer them as for example students.get(0)
@ScopeY for (Student s : students) { (do something here) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.