ScopeY
ScopeY

Reputation: 39

New variable for each user input in a loop

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 ...

Upvotes: 0

Views: 1920

Answers (2)

Salih Erikci
Salih Erikci

Reputation: 5087

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);
        }
    }
}

Upvotes: 0

radoh
radoh

Reputation: 4825

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;

Upvotes: 5

Related Questions