• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Highest/Lowest output from integer input (Beginner)

 
joseph james
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 score, gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
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.");
score = SavitchIn.readLineInt();
min = score;
max = min;
while (score > 0)
{
if (score >= 90) {
sumA = sumA + score;
gradeA++;
System.out.println("adding to a grades");
score = SavitchIn.readLineInt();
}
if ((score < 90)&&(score >= 80)) {
sumB = sumB + score;
gradeB++;
System.out.println("adding to b grades");
score = SavitchIn.readLineInt();
}
if ((score < 80)&&(score >= 70)) {
sumC = sumC + score;
gradeC++;
System.out.println("adding to c grades");
score = SavitchIn.readLineInt();
}
if ((score < 70)&&(score >= 60)) {
sumD = sumD + score;
gradeD++;
System.out.println("adding to d grades");
score = SavitchIn.readLineInt();
}
if (score < 60 && score > 0) {
sumF = sumF + score;
gradeF++;
System.out.println("adding to f grades");
score = SavitchIn.readLineInt();
}
if (score > max){
max = score;
}
if (score < min){
min = score;
}

}
double avgA = sumA/gradeA, avgB = sumB/gradeB, avgC = sumC/gradeC,
avgD = sumD/gradeD;
double avgF = sumF/gradeF;
totalNumber = (gradeA + gradeB + gradeC + gradeD + gradeF);
System.out.println("The total number of grades is " + totalNumber);
System.out.println("The number of A grades is " + gradeA);
System.out.println("The number of B grades is " + gradeB);
System.out.println("The number of C grades is " + gradeC);
System.out.println("The number of D grades is " + gradeD);
System.out.println("The number of F grades is " + gradeF);
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error response do you get?
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using arrays to find the highest and lowest can be easier.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic