I would say its not at the end of the code, more like all over, but no offense,
I just have a feeling that you are quite new to algorithms and computing.
Have a look at this extreme makeover that I have done to your code.
The reason for me using the getKeyboardInt method is explained in the
comment.
Code:
import java.io.*;
public class project64 { //Project 6-4
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
/**
* Show user prompt and get integer input
* (I use this since I haven't got the TerminalIO.KeyboardReader)
* @param prompt
* @return
*/
private int getKeyboardInt(String prompt) {
String line=null;
while (true) {
System.out.println(prompt);
try {
line = keyboard.readLine();
int ret = Integer.parseInt(line.trim());
return ret;
}
catch (IOException ex) {
System.out.println("An error occurred");
}
catch (NumberFormatException ex) {
System.out.println(line+" is not an integer, please reenter");
}
}
}
/**
* check a value and return the corresponding letter
* @param d
* @return
*/
private char getLetter(double d) {
if (d >= 89.5)
return 'A';
else if (d >= 79.5)
return 'B';
else if (d >= 69.5)
return 'C';
else if (d >= 59.5)
return 'D';
else
return 'F';
}
/**
* Do the user input and accumulation
*/
public void doIt() {
int gradeTally=0; // sum of inputs of grades
int numGrades=0; // Number of grades inputed
double avg; // outputs class average
int grade; // grade entered by the user.
int minimum=Integer.MAX_VALUE; // outputs class minimum
int maximum=Integer.MIN_VALUE; // outputs class maximum
while (true) {
grade=getKeyboardInt("Enter a grade (value -1 to stop): ");
if (grade == -1) break;
gradeTally += grade;
numGrades++;
minimum=(grade < minimum) ? grade : minimum;
maximum=(grade > maximum) ? grade : maximum;
}
if (numGrades==0) {
System.out.println("No data was entered, program will terminate");
return;
}
avg = (double)gradeTally/(double) numGrades;
System.out.println("The average of the grades is:" + avg);
System.out.println("Letter Grade = " + getLetter(avg));
System.out.println("The class Minimum is " + minimum + " which is a: " +
getLetter((double)minimum));
System.out.println("The class Maximum is " + maximum + " which is a: " +
getLetter((double)maximum));
}
/**
* **************** MAIN ****************
* @param args
*/
public static void main(String[] args) {
project64 p64=new project64 ();
p64.doIt();
}
}
Bookmarks