Results 1 to 3 of 3
Thread: Problem with looping
- 02-16-2011, 04:34 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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).
Thanks in advance for any help you can give me!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 + "."); } }
- 02-16-2011, 04:48 AM #2
Nothing to do with looping.
First value entered is assigned to grade.Java Code:int grade = keyboard.nextInt(); int max = keyboard.nextInt();
Second value entered is assigned to max.
- 02-16-2011, 04:56 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Lottery help, looping problem
By hadoken5 in forum New To JavaReplies: 2Last Post: 10-31-2010, 06:48 PM -
Looping
By Dean29126 in forum New To JavaReplies: 3Last Post: 09-08-2010, 02:01 PM -
Problem With Looping [JAVA]
By jude113 in forum New To JavaReplies: 2Last Post: 03-06-2009, 01:00 PM -
problem with <logic:iterate> tag looping
By tsaswathy in forum Web FrameworksReplies: 0Last Post: 09-27-2008, 12:13 PM -
Looping problem
By Tanilo in forum New To JavaReplies: 1Last Post: 08-01-2008, 06:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks