Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: High / low integer computation algorithm????

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...


    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"));
     	}
     }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: High / low integer computation algorithm????

    Well, you could start by creating two more methods,
    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.

    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
    Last edited by Tjstretch; October 22nd, 2011 at 12:48 PM.

Similar Threads

  1. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM
  2. my High profitable items: 0 Bonus : $0.00 why??
    By chonch in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 10:31 PM
  3. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  4. Eclipse is always locked [High CPU & RAM usage]
    By talha06 in forum Java IDEs
    Replies: 4
    Last Post: March 16th, 2010, 10:07 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM