Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Highest/Lowest output from integer input (Beginner)

joseph james
Greenhorn
Posts: 3
When Im inputing multiple numbers I cant seem to get an output of the
highest number and the lowest number. This is a program for entering
grades. It seems to work if I enter in a score of 90 or higher but I get an
error response if a score is entered below 90. Thank you for any help you
can offer. This is for a beginning Java programming class. My code is:
public class James3b
{
public static void main(String[] args)
{
int sumA = 0, sumB = 0, sumC = 0, sumD = 0, sumF = 0;
int totalNumber, max, min;
System.out.println("Enter your scores. Enter a negative to end.");
min = score;
max = min;
while (score > 0)
{
if (score >= 90) {
sumA = sumA + score;
}
if ((score < 90)&&(score >= 80)) {
sumB = sumB + score;
}
if ((score < 80)&&(score >= 70)) {
sumC = sumC + score;
}
if ((score < 70)&&(score >= 60)) {
sumD = sumD + score;
}
if (score < 60 && score > 0) {
sumF = sumF + score;
}
if (score > max){
max = score;
}
if (score < min){
min = score;
}

}
System.out.println("The total number of grades is " + totalNumber);
System.out.println("The average for the A grades is " + (avgA));
System.out.println("The average for the B grades is " + (avgB));
System.out.println("The average for the C grades is " + (avgC));
System.out.println("The average for the D grades is " + (avgD));
System.out.println("The average for the F grades is " + (avgF));
System.out.println("The highest score is " + max);
System.out.println("The lowest score is " + min);
}
}

Ilja Preuss
author
Sheriff
Posts: 14112
What error response do you get?

James Chegwidden
Author
Ranch Hand
Posts: 201
Using arrays to find the highest and lowest can be easier.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by James Chegwidden:
Using arrays to find the highest and lowest can be easier.

Huh? In which way would that be easier???
There is, however, a shorter (and imho also better to read) replacement for the accompanied if statement:
min = Math.min(min, score);