• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Highest/Lowest output from integer input (Beginner)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error response do you get?
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using arrays to find the highest and lowest can be easier.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic