• Post Reply Bookmark Topic Watch Topic
  • New Topic

How I get my grade calculator to work properly?  RSS feed

 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so I have to create a grade calculator for my computer science class where the user can input as many grades as they want, then it will calculate the average for them and the equivalent letter grade. I have manage to let it allow me to input as many grades as I want, however, the prompt that says, "Enter grade number 1,2,3, or 4, etc." does not count up. The entire time it says enter grade number 1. Thats not my only problem, however. I also cannot get the part of my program that calculates the grade correct. If only one grade is put it, it will say the average is half of that. If too many are put in, it will give an average of well over 100%, even when no values over 100% are put in. I realy need help with fixing this ASAP. Here is my code:

 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Every time you enter the while loop, what will counter be?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The counter will be the the number identifying how many grades are put in. For example, "Enter grade number 1: 97 percent, next line, enter grade number 2:_". The counter will function both to identify to the user how many grades they have entered and it will be used to calculate the average. The average, as of right now, does not calculate correctly at all.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized that the code up there may be a little outdated. I fixed some minor spelling problems and made it a tiny clearer. Here is the code:

 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Every time you enter the while loop, what will counter be?

Look at the actual code.  What will counter be in line five every time you enter the loop?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The counter will only stay as 1 every time you enter the loop. I tried to create a for loop to fix that, but it only screwed up the rest of the code.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move counter = 1; above the while loop.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! Now that works. However, it still seems to be calculating my averages wrong. I will post a even further updated version of my code.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the most current version of my code:


 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard that last question. I figured it out. However, the sequence does not end at the break; command. Any ideas on that?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a break in the code.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I guess I had already removed it. I replaced it at the end of each with System.exit(0);, however I wanted it to have a break command there in order for it to just end the program, not exit it. But when I replace the exit(0); statement with break, the end of the code plays, but it does not end the program. It just continues having me enter grades.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got two while (YoN.equalsIgnoreCase("start")) loops -- one at line 49 and one at line 86.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but where would I put a break in order for it to end the while statements?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code has more serious problems than where to put a break.  It has repeated code and while loops that don't terminate.  Why don't you take a few minutes and write down in English or pseudocode how you'd like the program to work.  Break the code up into tasks.  Each task should probably be a method.  Get all your code out of the main() method.  See MainIsAPain (that's a link).  Start with something like this:

 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Beginning Java forum, we don't post complete solutions.  First, we are NotACodeMill (that's a link).  Second, we are here to guide the OP and help them learn.  We appreciate the effort and the urge to help, but please post only code snippets as illustration of your advice.
 
Leoncio Thiago
Greenhorn
Posts: 11
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In the Beginning Java forum, we don't post complete solutions.  First, we are NotACodeMill (that's a link).  Second, we are here to guide the OP and help them learn.  We appreciate the effort and the urge to help, but please post only code snippets as illustration of your advice.


I understand and respect it, but that post does not mean I am not trying to help them learning. I was trying to show where the issue was. And one thing that I believe is important on each topic that is raised is provide the final solution or the final understanding on the issue, so, not only the person that raised the issue , but all others that google it can learn.

I hope it helps the next moderation,
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final solutions can be posted by the OP, but not anyone else.  Please note that at the stated policy of this forum is:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!