High / low integer computation algorithm????
Okay, you guys were awesome , last time, here I am again, stuck. I am writing an exam averager, using a do-while loop with an if/else thrown in there. Its functional, but I also need it to display the highest and the lowest integer entered by the user. I have laid out a double highScore and double lowScore for the values to be assigned to, but I can't figure out how to make the program identify the highest and lowest values entered.
I think it will be something like an "if / else-if" type of loop inside the already established if / else loop, but I don't know what to tell it to look for. any guidance??? Code attached...
Code :
import java.util.Scanner;
/**computes the average of a user entered list of non-negative integers.
*Will repeat until user idcated to stop. Will indicate largest / smallest integer.
*/
public class ExamAverager
{
public static void main (String [] args)
{
System.out.println ("This program will compute the average of a list of");
System.out.println ("non-negative test scores.");
double sum;
int numberOfStudents;
double next;
double highScore;
double lowScore;
String answer;
Scanner keyboard = new Scanner (System.in);
do
{
System.out.println ();
System.out.println ("Enter all of the scores to be averaged.");
System.out.println ("Enter a negative number when complete, to see results.");
sum = 0;
numberOfStudents = 0;
next = keyboard.nextDouble ();
while (next >= 0)
{
sum = sum + next;
numberOfStudents++;
next = keyboard.nextDouble ();
}
if (numberOfStudents >= 0)
System.out.println ("The average is " + (sum / numberOfStudents));
else
System.out.println ("No scores to average.");
System.out.println ("Do you want to enter another test score?");
System.out.println ("Enter yes or no.");
answer = keyboard.next ();
}
while (answer.equalsIgnoreCase ("yes"));
}
}
Re: High / low integer computation algorithm????
Well, you could start by creating two more methods,
Code Java:
private double getHighestValue(ArrayList<Double> arr)
{
//Code eluded
}
private double getLowestValue(ArrayList<Double> arr)
{
//Code eluded
}
and try working on those. However, you are going to need to hold an array of the values given rather then just the some. Because you are not sure how many students there are going to be, may I recommend an ArrayList.
Code Java:
sum = 0;
numberOfStudents = 0;
next = keyboard.nextDouble ();
ArrayList<Double> studentScores = new ArrayList<Double>();
while (next >= 0)
{
studentScores.add(next);
sum = sum + next;
numberOfStudents++;
next = keyboard.nextDouble ();
}
Then work on the methods earlier described