Sorry for making two posts in such short amount of time but I really want to be "un-stumped" so to say on while loops and how to make them correct in what I need for a program. For this while loop, I am attempting to make it so that you can enter a bunch of tests and then figure out the average after you are done entering these tests. I also need to pick a sentinel which I had thought I did with the 999. This program doesn't really do anything for me. If I enter the 999 sentinel value it just seems to divide that by the number of entrances and not all of the inputs together divided by the number of inputs. Not really sure where I went wrong with this. I assume I might not have set up the sentinel value properly or some such thing. I am a little confused how to get the while loop to accept multiple inputs and then average them out. Since there is no set number of how many tests I will enter each time I run the program, I really can't just declare all of these tests as variables. Well, this one is messed up and I would appreciate the help thank you.
import java.util.*;
public class testLab2
{
public static void main(String[] args)
{
int count;
int testScore;
int total;
int average;
count = 0;
total = 0;
Scanner input = new Scanner(System.in); //1
System.out.println("Enter the Test Score"); //1
testScore = input.nextInt(); //1
while (testScore != 999) //2
{
if (testScore >= 90)
System.out.println("A");
else if (testScore >= 80)
System.out.println("B");
else if (testScore >= 70)
System.out.println("C");
else if (testScore >= 60)
System.out.println("D");
else
System.out.println("F");
count++; //3
total = total + testScore; //3
testScore = input.nextInt(); //3
if (count > 0)
{
average = testScore/count;
System.out.println("Test Average is: " + average);
}
}
}
}

New Topic/Question
Reply


MultiQuote





|