Bookmark Topic Watch Topic
  • New Topic

How do I make it so I don't have to preset the number of grades that can be entered?  RSS feed

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
I had to create a project for my computer science class, and I realized that my teacher wants the user to be able to input as many grades as they want. I created a class that has you set the number of grades that you are going to enter before hand, however I am not sure how I can give them the ability to put in as many grades as they want at once. I was thinking that you could create a while loop and have a key work trigger the end of the sequence, but I'm not sure how to do that. I want to keep the prompt before the user enters their grade of "Enter grade number (#)" before the user enters their grade. Then it should go on and one until a keyword prompts it. I am rather new to coding, only just started the class this year. Here is my code:


 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Also how could I create a message if someone input more than 100%, for it to ask them if they are sure this is the correct value and if they respond yes, it keeps the value, and if no, it makes them re enter it?
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator

Both questions can be answered using a while loop (or a do/while loop).  It looks like you know how to code a while loop, so let's start with that.  How would a user entering grades, tell the program they want to stop?  Press the <enter> key?  Enter -1?  Give it some thought and try stuff out.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
I would suggest you shou‍ld move most of that code out of the main method. I think you shou‍ld have a result class, which incorporates the grades, calculates the results, etc.
I would also suggest you use else if rather than straight if statements.
Also consider the use of operators. The >= and <= operators are harder to understand that < and >. Also, 100 > average is much more difficult to understand than average < 100. You can make if statements really easy to understand or really difficult to understand by the operators you use.
Why 79.5 (etc)? What will you get for a mark of 79.6?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ive been trying different while loops for the past 2 hours, as that was what I thought might work as well, but I still am not getting it. How should my while loop for this instance start? Maybe that's where my error is.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Here is what I have so far. It only kind of works, allowing me to enter 1 number then I have to type end to end it.
Here is the code:
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
The thread seems to have gone here.  Locking this thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!