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;

Final int SENTINEL = -1;

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

Scanner scan = new scanner ( inputFile );

While ( != SENTINEL )

{
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;

/** 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

// Read scores and find the best score
for (int i = 0; i < scores.length; i++) {

scores[i] = input.nextInt();

if (scores[i] > best)

best = scores[i];

}

// Declare and initialize output string
String output = "";

for (int i = 0; i < scores.length; i++) {

if (scores[i] >= best - 10)

else if (scores[i] >= best - 20)

else if (scores[i] >= best - 30)

else if (scores[i] >= best - 40)

else

output += "Student " + i + " score is " +

}

// Display the result
System.out.println(output);

}``````

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

}

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)) {
}
Prompt the user for a number
}

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

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.

``````/**
*/

import java.util.Scanner;

/**
* Display the letter grade that corresponds to the
* numeric grade entered by the user.
*/

/**
* 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);
}
}
/**
* pre: 0 <= numGrade <= 100
* post: The letter grade A, B, C, D, or F has been returned.
*/

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;
Scanner input = new Scanner(System.in);

System.out.print("Enter a numeric grade (-1 to quit): ");