Member Avatar for mehnihma

I don't know what am I doing wrong,

When I input number of courses it won't loop again just once?

What is wrong with the code?

Thanks

// EX 3

import java.util.*; // program uses class Scanner

public class ComputeGpa {

    public static void main(String args[]) {
        // create Gpa object myGpa and 
        // pass grade to constructor

        Gpa myGpa = new Gpa("EX 3");

        myGpa.displayMessage(); // display welcome message
		  
		  Scanner in = new Scanner(System.in);  //scanner input


        double GPA = 0;
		  	char gradeCh = 'k';
  			int credits = -1;
			boolean isGradeCorrect = false;
			boolean isCreditsCorrect = false;

				System.out.println("\n");
          System.out.print("Enter Number of courses:  ");
          
           

            int coursNumber = in.nextInt();
				
				do // check number of courses
				if (coursNumber <= 0) 
				{
				System.out.println("Invalid number of courses - must be grater then 0");
				System.out.println("\n");
				System.out.print("Enter Number of courses:  ");
				coursNumber = in.nextInt();
				
				
				}while (coursNumber <= 0);
							
						
            
            System.out.println("\n");



        for (int am = 1; am <= coursNumber; am++) {
                   
            
            while(!isGradeCorrect) // check grade
				{
					System.out.print("Enter grade (one character) :   ");
	
            	String sGrade = in.next();
					gradeCh = sGrade.charAt(0);
					// validating input
					
					switch (gradeCh) {

             case 'a':
             case 'A': {
                 isGradeCorrect = true;
                 break;
             }
             case 'b':
             case 'B': {
                 isGradeCorrect = true;
                 break;
             }
             case 'c':
             case 'C': {
                 isGradeCorrect = true;
                 break;
             }
             case 'd':
             case 'D': {
                isGradeCorrect = true;
                 break;
             }
             case 'f':
             case 'F': {
                 isGradeCorrect = true;
                 break;
     				}
					default:
						System.out.print("Invalid grade - must enter exactly one lette");
						System.out.println("\n");
             }
				}

							
			
				 System.out.println();

            while(!isCreditsCorrect) // check credits
				{
					System.out.println("Enter Credits :   ");
            	credits = in.nextInt();
            	System.out.println("\n");
					
					if(credits >= 0 && credits <= 9)
					{
						isCreditsCorrect = true;
					}
					else
					{
						System.out.println("Invalid credits = must be between 0 and 9, inclusively");
						System.out.println("\n");
					}
				}

            myGpa.addTotals(gradeCh, credits);

            System.out.println("\n");


            GPA = myGpa.calcGpa();
				
				
				System.out.printf("GPA: %.2f", GPA);

        }
		



    } // end main
} // end

Recommended Answers

All 10 Replies

what exactly is the loop you're having trouble with, what is the input you're giving?
is it giving an error message?

Member Avatar for mehnihma

No there is no error

When I input 1 for course number then it is ok
but when I input 2 or more it just goes once and prints gpa four times.

It should print gpa calculation for x number of courses

In software development, you will find it very helpful to clearly describe the problem. It doesn't just help us help you; it will help you think clearly about the issue, and how to solve it.

So your problem is that once you enter correct values for grade and number of credits for the first course, it...

  1. Uses the same grade and number of credits for all other courses -- but you'd rather have it prompt the user for those values.
  2. It prints the "GPA" every time. But you probably want it to print this once, at the end.

Look at how you're "controlling" the inner loops. Make sure that you set or initialize your variables properly at the proper places.

well ... it is going over the loop again, but you'll need to reset the values of your
isGradeCorrect and isCreditsCorrect booleans

Member Avatar for mehnihma

how?

by (re-)setting the value to the one that will allow the loop to run again, either everytime at the beginning, or at the end of your outer loop

Personally, I always put variable declarations as close to their usage as possible. You get fewer bugs that way.

But using assignment statements would also work. I see that you know how to do those. ;->

Member Avatar for mehnihma

I did it :D
Thanks

the problem was

for (int am = 1; am <= coursNumber; am++) {
                   
            isGradeCorrect=false;
            isCreditsCorrect=false;
				
            while(!isGradeCorrect) // check grade
    {

and I needed to get last print statements out from the loop

jip :)

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.