jeudi 17 janvier 2019

How to find the average of randomly generated numbers in JAVA

I am randomly generating a GPA for a student in the class. In the app I repeat the step 8 times, for a total of 8 different GPA's. At the end of my print statement I want to calculate the total of all 8 GPA's. I only want it to print out once. Where should I put the steps for finding the average, in the class or in the app?

In the code I have multiple areas where I tried to find the average, but it did not print out correctly. I just made them into notes.

(this is in the public static void)

for(int i = 1; i < 9; i++ )
 {
     //System.out.println(sd.getInfo());
     System.out.println("Semester " + i + " - " + sd.semesterGPA());
     System.out.println();
 }
   System.out.println(sd.averageGPA());
 }

(this is in the public class student)

   String semesterGPA()
     {
       for(int i = 0; i < 1; i++)
     {         
          gpa = Math.random();
          myRandomNumber = (gpa * 4.1 );
          //String strDouble = String.format("%.2f", myRandomNumber);
          //average = (i/8);
          //System.out.println(firstName + "'s average GPA is " + average);
      }

    return firstName + "'s GPA is " +  String.format("%.2f", myRandomNumber);
    //System.out.println(firstName + "'s average GPA is " + average);
  }

String averageGPA()
{
   // double gpa = sum / count;

    return firstName + "'s average GPA is ";
}




Aucun commentaire:

Enregistrer un commentaire