Search code examples
javauser-input

Enter name and grades from user input to get average grade


I am working on a code where you have a students name and his grades and it prints his name total scores and his average scores. I want to make it more functional in allowing the user to input there name and scores and get all that info back. I may try to further this into a class average where it allows to input multiple individual names, scores, and average, print each one separately then showing the class average. This is a second thought though, as I need to have user input of grades and names first.

This is what I have so far. I have a separate class to make some things simpler.

public class Student {
    private String name;
    private int numOfQuizzes;
    private int totalScore;

    public Student(String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getTotalScore() {
        return totalScore;
    }
    public void addQuiz(int score) {
        totalScore = totalScore + score;
        numOfQuizzes++;
    }
    public double getAverageScore(){
        return totalScore/(double)numOfQuizzes;
    }
}

Then i have the main method:

public static void main(String[] args) {
    Scanner nameInput = new Scanner(System.in);
    System.out.print("What is your name? ");
    String name = nameInput.next();

    Student student = new Student(name);

    student.addQuiz(96);
    student.addQuiz(94);
    student.addQuiz(93);
    student.addQuiz(92);

    System.out.println("Students Name: " + student.getName());
    System.out.println("Total Quiz Scores: " + student.getTotalScore());
    System.out.println("Average Quiz Score: " + student.getAverageScore());
}   

This is what it prints out currently:

What is your name? Tom
Students Name: Tom
Total Quiz Scores: 375
Average Quiz Score: 93.75

UPDATE: This is what i have done so far. i added a loop and trying to use my student class but it doesn't seem to work with the array.

ArrayList<String> scores = new ArrayList<String>();
    Scanner nameInput = new Scanner(System.in);
    System.out.print("What is your name? ");
    String name = nameInput.next();

    Scanner scoreInput = new Scanner(System.in);

    while (true) {
        System.out.print("Please enter your scores (q to quit): ");

        String q = scoreInput.nextLine();

        scores.add(q);

          if (q.equals("q")) {
              scores.remove("q");

       Student student = new Student(name);

       System.out.println("Students Name: " + student.getName());
       System.out.println("Total Quiz Scores: " + student.getTotalScore());
       System.out.println("Average Quiz Score: " + student.getAverageScore());
       break;
    }
   }
  } 
}

Solution

  • Since you are only learning the language just yet, I'll give you a few suggestions to send you on your way. You might want to use a while-loop to consistently ask the user for a new grade until they've given you an empty line or something. You can reuse your scanner to read the new grades and they can be converted to integers using Integer.parseInt. Hopefully this'll allow you to write your code further, it's already looking great :).

    EDIT

    There are several points which can be improved upon in your edited code. The following shows some of them (using import java.io.Console;):

    Scanner inp = new Scanner(System.in);
    
    // Request the name
    System.out.print("What is your name? ");
    String name = inp.nextLine();
    
    // Create the student object
    Student student = new Student(name);
    
    // Ask for the grades
    System.out.print("Please enter your scores (q to quit): ");
    String grade = inp.nextLine();
    
    while (!grade.equals("q")) {
       // Convert the grade to an integer and pass it 
       student.addQuiz(Integer.parseInt(grade));
    
       // Request a new grade
       grade = inp.nextLine();
    }
    
    // Report the results
    System.out.println("Students Name: " + student.getName());
    System.out.println("Total Quiz Scores: " + student.getTotalScore());
    System.out.println("Average Quiz Score: " + student.getAverageScore());