Results 1 to 3 of 3
  1. #1
    jarredh4 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Problem with looping

    Hi everyone. I have an assignment for my Programming class due which I have been working on for the past couple of hours. The assignment is to take grades and output the minimum, maximum, and average grades. The minimum and maximum grades are working correctly. However, when taking the average of my grades, the program is ignoring the second grade entered every time. I have posted my code below (the output of sum and number of grades is for myself only and will be removed when I have completed the assignment).

    Java Code:
    import java.util.Scanner;
    
    public class Grading {
    
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		double sum = 0;
    		int numbergrades = 0;
    		System.out.println("Enter a nonnegative integer (negative to stop): ");
    		int grade = keyboard.nextInt();
    		int max = keyboard.nextInt();
    		int min = max;
    
    		while (grade >= 0) {
    			if (grade > 100) {
    				System.out.println("!!! Invalid value.");
    			} else if (grade > max) {
    				max = grade;
    				sum = sum + grade;
    				numbergrades = numbergrades + 1;
    			} else if (grade < min) {
    				min = grade;
    				sum = sum + grade;
    				numbergrades = numbergrades + 1;
    			} else {
    				sum = sum + grade;
    				numbergrades = numbergrades + 1;
    			}
    			grade = keyboard.nextInt();
    		}
    
    		double average = sum / numbergrades;
    		System.out.println(sum);
    		System.out.println(numbergrades);
    		System.out.println("The highest score is " + max + ".");
    		System.out.println("The lowest score is " + min + ".");
    		System.out.println("The average of the scores is " + average + ".");
    
    	}
    
    }
    Thanks in advance for any help you can give me!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Nothing to do with looping.
    Java Code:
    int grade = keyboard.nextInt();
    int max = keyboard.nextInt();
    First value entered is assigned to grade.
    Second value entered is assigned to max.

  3. #3
    jarredh4 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks so much! I was focused so much on the loop that I completely overlooked that error.

Similar Threads

  1. Lottery help, looping problem
    By hadoken5 in forum New To Java
    Replies: 2
    Last Post: 10-31-2010, 06:48 PM
  2. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  3. Problem With Looping [JAVA]
    By jude113 in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:00 PM
  4. problem with <logic:iterate> tag looping
    By tsaswathy in forum Web Frameworks
    Replies: 0
    Last Post: 09-27-2008, 12:13 PM
  5. Looping problem
    By Tanilo in forum New To Java
    Replies: 1
    Last Post: 08-01-2008, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •