my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Highest/Lowest output from integer input (Beginner) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Highest/Lowest output from integer input (Beginner)" Watch "Highest/Lowest output from integer input (Beginner)" New topic
Author

Highest/Lowest output from integer input (Beginner)

joseph james
Greenhorn

Joined: Nov 02, 2002
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 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

Joined: Jul 11, 2001
Posts: 14112
What error response do you get?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Using arrays to find the highest and lowest can be easier.


Author and Instructor, my book
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Highest/Lowest output from integer input (Beginner)