0

Beginner Java programmer here. I am trying to make a program that asks the user how many grades they would like to enter. Then, I want to store the grades the user entered into an array. Finally, I want to find the mean of the grades entered and print out a list of every grade below the mean. As of now, my code calculates the mean of the grades that the user enters but I cannot figure out how to print the grades below the mean. I believe the problem lies in my last for loop but I cannot figure out how to fix it. Also, did I implement the array correctly? Thanks to every who took the time to help me!

public static void grades() {
    int q = 0;
    double grades = 0;
    double total = 0;

    Scanner in = new Scanner(System.in);
    // user input how many grades user would like to enter
    System.out.println("How many grades would you like to enter? ");
    q = in.nextInt();

    // user enters # of grades they requested to enter
    for (int counter = 0; counter < q; counter++) { 
        System.out.println("Enter your grades: ");
        grades = in.nextInt();
        // This creates an array that stores the grades the user entered
        double[] scores = new double[] {grades};
        // adds up all elements (grades)
        for (int k = 0; k < scores.length; k++) {
                 total += scores[k]; //sums up entered grades
        }
    }
    total = total / q; //calcs mean
    //loops prints grades less than mean
    for (grades = 0; grades < total; grades++){
        System.out.println(grades);
    }
}
anto
  • 73
  • 2
  • 5
Speedo
  • 55
  • 2
  • 12

4 Answers4

1
public static void grades(){
    Scanner in = new Scanner(System.in);
    System.out.println("How many grades would you like to enter? "); //user input how many grades user would like to enter
    int q = in.nextInt();

    double[] grades = new double[q];
    double sum = 0;
    for (int counter = 0; counter < q; counter++){ //user enters # of grades they requested to enter
        System.out.println("Enter your grades: ");
        double grade = in.nextInt();
        grades[counter] = grade;
        sum += grade;
    }
    double mean = sum / q;
    System.out.println("Mean: " + mean);
    for (int i = 0; i < q; i++){ //loops prints grades less than mean
        if (grades[i] < mean) {
            System.out.println(grades[i]);
        }
    }
}
izilotti
  • 4,757
  • 1
  • 48
  • 55
0

Your last loop makes no sense

You should be counting through ALL the items in the array and then doing an IF to test if it's below the mean and then printing it if it is below the mean

You have made the loop iterate from 0 up to the mean.. that just makes no sense it's like saying the mean score among a group of students was 30% now print all the grades from student0 to student30 That makes no sense to do that. You should be interested in student 31 and student 32.. dont' stop at student number 30. student1 might have a grade above the mean and student 40 may have a grade below the mean. you're mixing up the student number(the index of the location in the array) with the grade.

ADDED You are actually doing even worse than that..because grades isn't even an array it is a variable with one number in it. You have an array that increments a variable and prints the variable like printing 1,2,3,4,5

You're meant to do a loop i=1 to n, and print array[i] You are doing a loop i=1 to n and you are printing i

You should figure out what an array is all about rather than worrying about calculating a mean. Make your own simpler exercises testing what you do/don't understand rather than something out of a book or something the teacher gave you. And use your explorations to understand this exercise your teacher/book gave you.

barlop
  • 12,887
  • 8
  • 80
  • 109
0

Your code had some issues. First off your array was being initialized with every iteration for a new grade being entered, which would result in one index since the previous inputs were overriding it. The array scores should be initialized outside the for loop.

Also, your for loop for printing out the grades wasn't correct. You needed to iterate over the scores array and print out each individual value.

The code below works and demonstrates where you went wrong:

public static void grades() {
    int q = 0;
    double grades = 0;
    double total = 0;

    Scanner in = new Scanner(System.in);
    // user input how many grades user would like to enter
    System.out.println("How many grades would you like to enter? ");
    q = in.nextInt();
    double[] scores = new double[q];//initialize here
    // user enters # of grades they requested to enter
    for (int i = 0; i < scores.length; i++) {
        System.out.println("Enter your grades: ");
        grades = in.nextInt();
        // This creates an array that stores the grades the user entered
        scores[i] = grades;//append values to each index
        // adds up all elements (grades)
        total += scores[i]; //sums up entered grades
    }
    total = total / q; //calcs mean
    System.out.println("Mean: " + total);
    //iterate through the scores array with filled values
    for (int i = 0; i < scores.length; i++) {
        System.out.println("Grade #" + i + ": " + scores[i]);
    }
}
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
0

what might be the problem of that .. the total and average is wrong.

import java.io.*;

 class overloading2
{
    String name;
    int year;
    String section;
    String subject[];
    double grade[];
    double average;


    public void enrolSubjects()throws Exception
    {
        DataInputStream mat=new DataInputStream(System.in);
        try
        {

            System.out.println("Enter 3 subject:");
            subject=new String[3];

            for(int sub=0;sub<3;sub++)
            {
                subject[2]=(mat.readLine());
            }   
        }
        catch(IOException ioe)
        {   
        }
    }
    public double enterGrades(double grade1,double grade2,double  grade3)
    {
        DataInputStream math=new DataInputStream(System.in);
        try
        {
            System.out.println("Enter 3 grades:");
            grade=new double[3];

            for(int x=0; x<3; x++)
            {
                grade[2]=Double.parseDouble(math.readLine());
            }   
        }

        catch(IOException ioe)
        {

        }
        return enterGrades();
    }
    double enterGrades()
            {
                    double total=0.0;
                    for(int x=0; x<3; x++)
                    {

                            total+=grade[2];
                            average=total/3;
                    }

                    return average;
            }
    public static void main(String []args)throws Exception
    {
        overloading2 ostud1=new overloading2();
        double a=0.0,b=0.0,c=0.0;
        ostud1.enrolSubjects();
        ostud1.enterGrades(a,b,c);
        System.out.println("average grade is "+ostud1.enterGrades());

    }

}
Robert
  • 5,278
  • 43
  • 65
  • 115