Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 02-28-2012, 06:22 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:40 AM.


Advertisement
Log in to turn off these ads.