Ok, I am having trouble getting this loop to continue. It just asks once and that's it. Here is my code:

// ----------------------------------------------------------
//   ExamScores.java (Application)
//
//   Author:        
//   Entered by:    --  --
//   Classes:       ExamScores (list all the class files related to this problem)
//   Date:          September 22, 2008
//   Description:    Creating a loop to receive input of grades and outputing the subsequent letter grade
//   Bugs:          No known bugs.
// ----------------------------------------------------------
import java.util.*;

public class ExamScores 
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        int gradeCounter;
        int A=0;
        int B=0;
        int C=0;
        int D=0;
        int F=0;
        double grade =0;

        gradeCounter = 0;  

        System.out.print("Enter an exam score (to quit enter a negative score): ");
        grade = input.nextDouble();      

        while (grade !=-1);
        {           
            System.out.println("Enter an exam score (to quit enter a negative score): ");
            grade = input.nextDouble();

            if(grade >=90)
                A++;
            else 
                if(grade >=80)
                B++;
            else 
                if(grade >=70)
                C++;
            else 
                if(grade >=60)
                D++;
            else
                F++;

            gradeCounter = gradeCounter + 1;

            grade++;
        }// end of while loop

        System.out.println("A: " + A +"\nB: " + B + "\nC: " + C + "\nD: " + D + "\nF: " + F );
    }
}

Recommended Answers

All 5 Replies

Remive the semi-colon by your while-statement and you should be in business =)

commented: The more I see posts like above the more I feel good debuggers are breed nearing extinction :P !!! +3
commented: Good Catch +2

Remive the semi-colon by your while-statement and you should be in business =)

I removed the semicolon and now it will keep prompting me to enter a grade. The problem now is, when I enter a -1 it doesn't stop the infinite loop.

Also, I want to be able to enter a negative number (not just a -1) to stop the loop. Would it be possible to do this statement?: while (grade !<=-1)
Thanks in advance!

Thats cause you are doing a grade++; near the end of your loop. Try entering -2 and it should stop.
But I do not see any point in grade++; being there since you are anyways going to overwrite it with grade = input.nextDouble(); on the next iteration.

And please use code tags. Look here and here for more information on them. You should read the rules before posting.

Also, I want to be able to enter a negative number (not just a -1) to stop the loop. Would it be possible to do this statement?: while (grade !<=-1)
Thanks in advance!

try

while (grade >= 0)

Ok, I am having trouble getting this loop to continue. It just asks once and that's it. Here is my code:

// ----------------------------------------------------------
//   ExamScores.java (Application)
//
//   Author:        
//   Entered by:    --  --
//   Classes:       ExamScores (list all the class files related to this problem)
//   Date:          September 22, 2008
//   Description:    Creating a loop to receive input of grades and outputing the subsequent letter grade
//   Bugs:          No known bugs.
// ----------------------------------------------------------
import java.util.*;

public class ExamScores 
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        int gradeCounter;
        int A=0;
        int B=0;
        int C=0;
        int D=0;
        int F=0;
        double grade =0;

        gradeCounter = 0;  

        System.out.print("Enter an exam score (to quit enter a negative score): ");
        grade = input.nextDouble();      

        while (grade !=-1);
        {           
            System.out.println("Enter an exam score (to quit enter a negative score): ");
            grade = input.nextDouble();

            if(grade >=90)
                A++;
            else 
                if(grade >=80)
                B++;
            else 
                if(grade >=70)
                C++;
            else 
                if(grade >=60)
                D++;
            else
                F++;

            gradeCounter = gradeCounter + 1;

            grade++;
        }// end of while loop

        System.out.println("A: " + A +"\nB: " + B + "\nC: " + C + "\nD: " + D + "\nF: " + F );
    }
} 

end quote.

Your program is executing correctly as written. There needs to be a way of returning to the top either through another prompt to the user or by modifying your case to return or somehow include your gradeCounter variable to actually control the loop...

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.