assigning grades

assign a grade to a score

Page 1 of 1

3 Replies - 10357 Views - Last Post: 06 April 2010 - 07:29 PM Rate Topic: -----

#1 shantalg5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-March 10

assigning grades

Posted 06 April 2010 - 07:16 PM

** THE PROBLEM that i need help with is that its not letting me insert the scores how can I insert my scores??

I'm writing a program that reads students scores, gets the best score, and then assigns grades based on
Grade is A if score is >= best - 10;
Grade is B if score is >= best - 20;
Grade is C if score is >= best - 30;
Grade is D if score is >= best - 40;
Grade is F otherwise;


Here's the sample run:

Enter the number of students: 4
Enter 4 scores: 40 55 70 58

Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
__________
here's what I have so far:
import java.util.Scanner;
public class assigningGrades {
/** Main method */
    public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);
    // Get number of students
    System.out.print("Enter number of students: ");
    int numberOfStudents = input.nextInt();

    int[] scores = new int[numberOfStudents]; // Array scores
    int best = 0; // The best score
    char grade; // The grade

     // Read scores and find the best score
    System.out.print("Enter " + numberOfStudents + " scores: ");
    
    for (int i = 0; i < scores.length; i++) {
        if ( scores[i] > best)
             best = scores[i];
    
//find the highest score

    }
    // Declare and initialize output string
    String output = "";
    // Assign and display grades
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] >= best - 10)
            grade = 'A';
        else if (scores[i] >= best - 20)
            grade = 'B';
        else if (scores[i] >= best - 30)
            grade = 'C';
        else if (scores[i] >= best - 40)
            grade = 'D';
        else
            grade = 'F';

output += "Student " + i + " score is " +
 scores[i] + " and grade is " + grade + "\n";
}
// Display the result
 System.out.println(output);
}
}


Is This A Good Question/Topic? 1
  • +

Replies To: assigning grades

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: assigning grades

Posted 06 April 2010 - 07:20 PM

You never use your Scanner to get the numberOfStudents grade inputs.

Try putting a for loop to read in the values after this line: System.out.print("Enter " + numberOfStudents + " scores: ");

for(int i = 0; i < numberOfStudents; i++){
    //get int value from Scanner.
    //store it in scores[i]
}

This post has been edited by erik.price: 06 April 2010 - 07:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: assigning grades

Posted 06 April 2010 - 07:21 PM

use a for loop to iterate the number of scores you want to insert, and for each iteration get an int value using the scanner Object, and assign it to the current index of the loop:
for(int i = 0 ; i < scores.length;i++){
   scores[i] = input.nextInt();
}


edit, ninjad by erik.price

This post has been edited by japanir: 06 April 2010 - 07:22 PM

Was This Post Helpful? 1
  • +
  • -

#4 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: assigning grades

Posted 06 April 2010 - 07:29 PM

you are not entering the scores in the array elements
try this
System.out.print("Enter " + numberOfStudents + " scores: ");
	for (int i = 0; i < scores.length ; i++  ){
		System.out.println("Enter the score");
		scores[i] = input.nextInt();
	}


and I want to suggest some java naming standards ,the convention is called as camelCase style
for class name, it should always start with a Capital letter and if you want to join two names, then the first name of should be capital in your example write like AssigningGrades
for methods and variables, start with small letter and if there is another word then capitalize the first letter of second word like methodName

This post has been edited by pdkharkar: 06 April 2010 - 07:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1