...

View Full Version : Array loops and while confusment



myironworker
02-28-2012, 04:09 AM
Below is the code, and current print out.

What I need todo is have the ability to have the input record be any vaule, other words onetime 5 records the next maybe 50.

And should the printf be change too println?

Is there away to add a print that displays A=2 B=1 C=1 D=2 F=1


import java.io.FileReader;
import java.util.Scanner;

public class Grades {
static public void main(String[] args) throws Exception {

int avg = 0;
Scanner input = new Scanner(new FileReader("grades.txt"));

int[] numGrade = new int[5];
String[] name = new String[5]; // stores name of each student
int[] avgMarks = new int[5]; // stores avg marks of each student
String[] letterGrade = new String[5]; // stores grade of each student
int totalMarks =0; // stores total marks

for (int i = 0; i < 5; i++) {
name[i] = input.next();
avg = 0;
for (int j = 0; j<3; j++) {
numGrade[j] = input.nextInt();
avg += numGrade[j];
if (j == 2) {
avgMarks[i] = avg/3;
totalMarks += avgMarks[i];
}
}

}

// get the grade

for (int i=0; i<5; i++) {

if (avgMarks[i] < 60) {
letterGrade[i] = "F";

} else if (avgMarks[i] < 70) {
letterGrade[i] = "D";

} else if (avgMarks[i] < 80) {
letterGrade[i] = "C";

} else if (avgMarks[i] < 90){
letterGrade[i] = "B";

} else {
letterGrade[i] = "A";
}
}

for (int i = 0; i<5; i++) {
System.out.printf("%s\t has an average grade of %d%%. You will receive a %s in this class. \n", name[i], avgMarks[i], letterGrade[i]);
}

System.out.printf("\nOverall Class Average is %d%%.",totalMarks/5);



//generates three random numbers between 0 and 50, calculates the average.
int one,two,three;
one=(int)(Math.random()*(51));
two=(int)(Math.random()*(51));
three=(int)(Math.random()*(51));

int average=(one+two+three)/3;
System.out.println("\n\nThe following program generates 3 random numbers, then averages them.");
System.out.println("\nThe average of\t"+one+" "+two+" "+three+" is "+average);



}

}

Which prints the following

Connie has an average grade of 85%. You will receive a B in this class.
James has an average grade of 92%. You will receive a A in this class.
Susan has an average grade of 52%. You will receive a F in this class.
Jake has an average grade of 66%. You will receive a D in this class.
Karen has an average grade of 77%. You will receive a C in this class.

Overall Class Average is 74%.

The following program generates 3 random numbers, then averages them.

The average of 46 44 21 is 37

Fou-Lu
02-28-2012, 07:22 AM
You need to resize your arrays if you plan on using a primitive array. This requires manually allocating a new array size and copying the existing data into the new array and discarding the original. The easier solution is to use an ArrayList as a collection, or a Hashtable to associate name => grades (Hashtable<String, ArrayList<Integer>>). That only works if the 'name' is unique.
Printf versus println is really irrelevant. The only difference is that printf requires \n to be added for linefeeds.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum