Hello, so here is the program that I am trying to create: write a program to read a collection of exam scores, ranging in value from 0 to 100, until a sentinel of -1 should display the numer of outstanding scores (90-100), the number of satisfactory scores (60-89), and the number of unsatisfactory scores (1-59). It should also prompt the user if they want to see the averages and the percentage of students with satisfactory scores or above for all scores entered. I have gotten the first part done just fine, but I am stuck with the prompt of asking the user if they want to see the averages and percentages. Here is what I have so far:

import java.util.Scanner; //Needed for user input 
import java.util.ArrayList; //Needed for array list

//This program calculates a collection of exam scores
//and gives outstanding, satisfactory, unsatisfactory, averages, and passing rates

public class examScores

   public static void main(String[] args)
   {
      //variables to hold exam scores.
      int outstandingScore = 0;
      int satisfactoryScore = 0;
      int unsatisfactoryScore = 0;
      int count = 0;

      //create keyboard for input
      Scanner in = new Scanner(System.in);


      //Ask user to input exam score.
      System.out.println("Enter a list of exam scores from 0-100 ");
      System.out.println("Enter -1 if you are done with your list : ");
      int score = in.nextInt();

//While loop for determination of scores
while(score>0)
   {
      count++;
if(score>=90 && score<=100)
    outstandingScore++;

   else if(score>=60 && score<=89)
    satisfactoryScore++;

   else if(score>=1 && score<=59);
    unsatisfactoryScore++;
    score = in.nextInt();
}//end while

System.out.println("Total number of grades: " + count);
System.out.println("Total number of outstanding scores: " + outstandingScore);
System.out.println("Total number of satisfactory scores: " + satisfactoryScore);
System.out.println("Total number of unsatisfactory scores: " + unsatisfactoryScore);



   String input;     //To hold keyboard input
   double averages = 0;  //Average of exam scores
   char prompt;      //Holds 'y' or 'n'
   double total = 0;    //Total number of grades
   int numInputs = 0;   //Total number of scores entered by user
   double scores = 0;


   //Create a scanner object for keyboard input
   Scanner keyboard = new Scanner(System.in);

 System.out.println("Do you want to see the averages and passing rates of all scores? ");
   System.out.print("Enter Y for yes or N for no: ");
   prompt = keyboard.next().charAt(0);


//While loop if prompt is "yes"
if(prompt == 'Y' || prompt == 'y'){

while(prompt == 'Y' || prompt == 'y');
{  
      numInputs++;
      averages = total/numInputs;

if(scores>=100 && scores<=60)
   outstandingScore++;
   satisfactoryScore++;

   if(score>=1 && score<=59);
    unsatisfactoryScore++;
    score = in.nextInt();

}//end while

}
  System.out.println("The average is" + averages);
  System.out.println("The percentage of students with satisfactory scores "
                     + "or above is: " + count);
   }

Everything runs great until I hit "Y" to see the averages, and then it just stops there.....any help is greatly appreciated!

Recommended Answers

All 4 Replies

Member Avatar for RudyM

After a very brief review, line 67: while(prompt == 'Y' || prompt == 'y'); is its own statement that loops forever. Try removing the semicolon? Basically you're just saying while(condition), just loop. So it never gets to the intended body following the loop condition.

I just tried it and it did not change anything, still getting the same output............

You have the same problem on lines 36 and 76. You have quite a few bracket problems. You have an unneeded if statement on line 65 and a comment on line 64 that seems to refer to line 67, not line 65. The indentation and code on line 74 suggests that it is part of the if-block starting on line 72. However, it is not. Again, brackets.

Proper indentation will let us (and you) find the missing brackets. When I see indented code, I should be able to go backwards and find an if-statement or a loop or something else that justifies that indentation as part of a block of code. Or it should be clear that it is indented because it is part of a single statement that is too long to fit in a single line. Bracket placement and indentation styles vary, but there should be some consistency. Again, this is for your own benefit as well as anyone else reading the code.

As to your loop starting on line 67, figure out what code should be inside it and stick it in brackets. Look at that code. The program got inside that loop because the loop condition was true. To get OUT of the loop, something inside that block of code must change the loop condition so that it is now false. Otherwise, infinite loop.

Member Avatar for RudyM

I ALWAYS follow conditional statements with a brackets { ... }, even if it's just one-line of code. Not a bad practice for me at least.

commented: Yes. A very good practice - avoids all kinds of errors +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.