Hello,

I've worked on some code for a project I have to do but I'm a little unsure about a few parts that I was hoping somebody could provide some clarity on. Here is the project that I'm working on with the code and question on it to follow:

Write a program to read a list of exam scores given as integer percentages in the range of 0 to 100. Display the total number of grades and the number of grades in each letter grade category as follows:
90-100 is an A
80-89 is a B
70-79 is a C
60-69 is a D
0-59 is a F
Use a negative score as a sentinel value to indicate the end of the input. The negative value is used only to end the loop, so do not use it in the calculations.

The output example:
Total number of grades = 8
Number of A's =4
Number of B's = 4

Here is what I have so far:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;


  String gradeS;
  char grade;
  Final int SENTINEL = -1;

  File inputFile = new File ( "input.txt");

  Scanner scan = new scanner ( inputFile );

      While ( != SENTINEL )

            switch (grade)
       {
             case 'A':

             break;
           
             case 'B':

             break;

             case 'C':

             break;

            case 'D':

            break;

           case 'F':

           break;

  System.out.println ( "Total number of grades = " +                      )

  System.out.println ( "Number of A's = " +                           )

   System.out.println ( "Number of B's = " +                           )

I know that my code is far from complete but this is what I was able to get through so far. I'm fairly sure that I need to put a counter at the beginning when I declare my variables and then implement that counter within the switch statement to add the number of grades, but I'm unsure how to declare that and implement it. I also think I may need an if statement within the switch statement... something like case 'A'
if ( 90-100)
else
case 'B'
if (80-89)
etc.....
I appreciate any guidance that can be provided on helping me move forward on this. Thanks again.
case 'b'

I am not understanding what you are asking... but if you are looking for the if statements here you go... be sure to change the code as needed

if (bmi < 70)
      System.out.println("F");
    else if (bmi < 80)
      System.out.println("C");
    else if (bmi < 90)
      System.out.println("B");
    else if (bmi < 100)
      System.out.println("A");
  }

Have a look at my code, hope I was of help. I used an array and a for loop...if you have to use a switch statement just edit the coding

import java.util.Scanner;



public class AssignGrade {

  /** Main method */

  public static void main(String[] args) {

    // Create a Scanner
    Scanner input = new Scanner(System.in);



    // Get number of students
    System.out.print("Please 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
    for (int i = 0; i < scores.length; i++) {

      System.out.print("Please enter a score: ");

      scores[i] = input.nextInt();



      if (scores[i] > best)

        best = scores[i];

    }



    // 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);

  }

Edited 7 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code]

That helps a bit. Thanks. The program must read from an input file and stopped by a negative number though, rather than asking for input and taking the user input.

Also, it wasn't required to use a switch statement it's just that we hadn't covered array's yet (go figure it's the next chapter) and I couldn't wrap my head around it enough to try to implement it.

So, this helps to move me in the right direction. I'm still just a bit unsure how I should implement the SENTINEL (the negative digit that stops the file) and what is the most logical way to count the total grades and each grade counted.

Using a for loop seems more logical
++ means add 1 each time the loop is ran
-- means subtract 1 each time the loop is ran

(for int i = (Starting integer); i > 0; i--) {

if statements for finding grade go here
totalgrade++


}

Sigh, if you want the whole code, ask for it...Since it's pretty long.

But this is HOW to solve it. I'm sure you can follow this to find the answer.

main()
Prompt the user for a number
while (number != -1) {
if (isValidNumber(number)) {
getLetterGrade(number)
Display message with letter grade
}
Prompt the user for a number
}

isValidNumber(userNum, maxNum, minNum)
if (minNum <= userNum <= maxNum) {
return (true);
} else {
return (false);
}

getLetterGrade (numericGrade)
if (numericGrade < 60) {
return ("F");
} else if (numericGrade <70) {
return ("D");
} else if (numericGrade <80) {
return ("C");
} else if (numericGrade <90) {
return ("B");
} else {
return ("A");

You will need a loop structure to get the user's input and two ADDITIONAL methods... One method will determine if the input is valid and another determines which letter corresponds to the numeric grade.

I did this code out... Hope it helps.

/**
 * GradeConverter
 */

import java.util.Scanner;

/**
 * Display the letter grade that corresponds to the 
 * numeric grade entered by the user.
 */
 
public class GradeConverter {

/**
 * Determines if a numeric entry is valid.
 * pre: none
 * post: true has been returned if minNum <= userNum <= maxNum;
 * false has been returned otherwise
 */
 
 public static boolean isValidNumber(int userNum, int minNum, int maxNum) {
 	if (minNum <= userNum && userNum <= maxNum) {
 		return(true);
 	} else {
 		return(false);
 	}
 }
 /**
  * Determines the letter grade that corresponds to the numeric grade.
  * pre: 0 <= numGrade <= 100
  * post: The letter grade A, B, C, D, or F has been returned.
  */
  
    public static String getLetterGrade(int numGrade) {
    	if (numericGrade < 60) {
    		return ("F");
    	} else if (numericGrade <70) {
    		return ("D");
    	} else if (numericGrade <80) {
    		return ("C");
    	} else if (numericGrade <90) {
    		return ("B");
    	} else {
    		return ("A");
    }
}

public static void main(String[] args) {
	final int SENTINEL = -1;
	final int minValue = 0;
	final int maxValue = 100;
	int numericGrade;
	String letterGrade;
	Scanner input = new Scanner(System.in);
	
	System.out.print("Enter a numeric grade (-1 to quit): ");
	numericGrade = input.nextInt();
	while (numericGrade != SENTINEL) {
		if (isValidNumber(numericGrade, minValue, maxValue)) {
			letterGrade = getLetterGrade(numericGrade);
			System.out.println("The grade " + numericGrade + " is a(n) " + letterGrade + ".");
		} else {
			System.out.println("Grade entered is not valid.");
			numericGrade = input.nextInt();
		}
	}
}
}

Might have a few errors... but here it is.

This article has been dead for over six months. Start a new discussion instead.