Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sorting out my program

807599Feb 21 2007 — edited Feb 22 2007
Hello everyone,
I am very new to java and I am trying to make a program which gives you the average of a list of integers, as well as the highest and lowest integer in the list. The program prompts the user to input a list of positive integers followed by a negative sentinel to end the program. As of now my program gives the highest and lowest score, but not the average. I believe this is because when going through the first loop the negative number shuts down the second half of the program. I need help connecting these loops in some way. Any help would be greatly appreciated. Anyway, here is what I have:
 import java.util.*; 

public class List {
	public static void main(String[] args)
	{

		System.out.println("Enter a list of numbers. All of the\n numbers must be nonnegative integers.\n Place a negative integer at the end\n to mark the end of the list."); 
		System.out.println("This Program will output the\n highest, lowest, and average\n of all of the nonnegative integers\n you enter."); 
		Scanner keyboard =  new Scanner(System.in); 
		int number, max, count = 0;  
		double sum = 0;
		max = keyboard.nextInt();  
		number = keyboard.nextInt();  
		int number1 = number;
		int min = max;
		
		while (number >= 0)
		{ 
			if (number > max) 
				max = number; 
			if (number < min) 
				min = number; 
			number =keyboard.nextInt(); 
			} 

		System.out.println("The highest integer is " + max); 
		System.out.println("The lowest nonnegative integer is " + min); 
		
		while (number >= 0)
		{ 
			sum = sum + number1; 
			count++; 
			number1 = keyboard.nextInt();
		} 
		 
		if (count > 0) 
			System.out.println("The average of your nonnegative integers is " + (sum/count)); 
		else 
			System.out.println("No numbers to average."); 

	}

}
thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2007
Added on Feb 21 2007
6 comments
208 views