View Single Post
Old 02-28-2012, 03:09 AM   PM User | #1
myironworker
New to the CF scene

 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
myironworker is an unknown quantity at this point
Array loops and while confusment

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

Code:
 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
myironworker is offline   Reply With Quote