Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - How to calculate the average grade of the values from an array?

Please help me..I've been working on this nonstop and i still cant get this to run the way i want it to.I am new to programming. i am not very good at using methods. i have a sample of my work here and my problem is how do i get the average of the grades and how do i print it on the console. when i run this code the last values inside the variable grade are the only values that display on the console. I also need to check the attendance...how do i do it? i need this code for my finals.

package studentmonitoring;

import java.util.*; public class StudentMonitoring {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);

 Date date=new Date();
  int[]  attend1={},attend2,attend3,grade={};
 String line="-------------------------------------------------------------------";
String[] idNum1={},idNum2={},gender={}, name1={},name2={},name3={},name4={},address={},sub={};

Scanner input=new Scanner(System.in); int check=0,num=0;

System.out.println(line);
System.out.println("		****STUDENT MONITORING****");
System.out.println(line);

  System.out.println("
Good day teacher, to start using this program input the following information first.
");
    System.out.println(line);
    System.out.print("NAME OF SCHOOL: ");
    String sName=in.nextLine();
    System.out.print("ADDRESS OF SCHOOL: ");
    String sAdd=in.nextLine();
    System.out.print("SCHOOL IDENTIFICATION NUMBER: ");
    String sId=in.nextLine();
    System.out.println(line);

    System.out.println("STUDENTS INFORMATION");
    System.out.println("Enter the year and section of this class");
    String yearSec=in.nextLine();
    System.out.println("How many student information do you want to input? ");
    int numberStudents=in.nextInt();


     idNum1=new String[numberStudents];
     name1=new String[numberStudents];
     name2=new String[numberStudents];
     name3=new String[numberStudents];
     name4=new String[numberStudents];
     attend1=new int[200];
     attend2=new int[200];
     attend3=new int[200];
     address=new String[numberStudents];
     grade=new int[100];
     gender=new String[numberStudents];
      idNum2=new String[numberStudents];


    System.out.println("Enter the number of subjects you have: "); 
   int numSub=in.nextInt();

   sub=new String[numSub];
   in.nextLine();

   for(int x=0;x<numSub;x++)  {
    System.out.print("Subjec #"+(x+1)+": ");
   sub[x]=in.nextLine();
   }  
    for(int x=0;x<numberStudents;x++)  {
        in.nextLine();
        System.out.println(line);
        System.out.println("Student #"+(x+1));
        System.out.print("LAST NAME: ");
        name1[x]=in.nextLine(); 
        System.out.print("FIRST NAME: ");
        name2[x]=in.nextLine();
        System.out.print("MIDDLE INITIAL: ");
        name3[x]=in.nextLine(); 

        System.out.print("GENDER: ");
        gender[x]=in.nextLine();

        System.out.print("ADDRESS: ");
        address[x]=in.nextLine();

        System.out.print("ID NUMBER: ");
        idNum1[x]=in.nextLine();


        System.out.println(line);
        System.out.println("Enter the grades here.");
        System.out.println(line);
         for (int a=0;a<numSub;a++)  {
          System.out.print(sub[a]+": "); 
         grade[a]=in.nextInt();
         }
        }



    System.out.println("This is the list of the students info you've inputted.");
        System.out.println("ID Number	    Name		   GENDER		    ADDRESS");


        for(int x=0;x<numberStudents;x++)  {

        System.out.println(idNum1[x]+"		"+name1[x]+" "+name2[x]+","+name3[x]+".		"+gender[x]+"		"+address[x]);
        }

     System.out.println("Subject		Grades");
     for(int x=0;x<numSub;x++)  {
         System.out.println(sub[x]+"		"+grade[x]);
     }
     System.out.println(line);

     System.out.println();
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
public static void main(String[] args) {
    System.out.println("Enter the number of students you want: ");
    Scanner scanner = new Scanner(System.in);

    int numOfStudents = scanner.nextInt();

    double[] grades = new double[numOfStudents];

    double total = 0;
    double currentGrade = 0;

    for(int i=0; i<grades.length; i++) {
        System.out.println("Grade achieved: ");
        currentGrade = scanner.nextDouble();
        grades[i] = currentGrade;
        total += currentGrade;
    }

    scanner.close();
    System.out.println(Arrays.toString(grades));
    System.out.println("Average grade " + (total / numOfStudents));
}

Here we first set the size of the double array (which will populate the grades). Then we fill in the double array using a for loop and getting user input via scanner.nextDouble().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...