• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Looping Input Help

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. Ok here is a bit of code I have so far.Im trying to write a program that has the user input a variety of grades. if the user enters a number not between 0 and 100 it gives an error message telling them to input a number between 0 and 100 and to keep continuing input numbers. When the user is finished I need the program to calculate the highest, lowest, and average grade of all the inputs.

My problem is i'm unsure of how to read each input from the scanner individually to be able to calculate these things. I've written other programs having a set number of variables, but i'm lost when it comes to a user generated amount of variables. any tips on how I would go about doing this? This is what I have so far. Thanks.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you surely need some kind of array of collection to store all the input grades. Then from this find the lowest, highest and average.

Also you maxValue and minValue can set to 100 and 0 respectively if you only allow inputs between 0 and 100.

Are you able to loop to get user inputs?
 
Marshal
Posts: 80136
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you create a utility class (search my posts for “utility class” and you should find some useful hints) and give it some methods
 
Campbell Ritchie
Marshal
Posts: 80136
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, wasn't my spelling bad in the last post
 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I was thinking arrays as well when I first saw my assignment but the professor stated

"You do not need to use array to save each input score for this lab. A Single
loop to read in each grade, compare with the current highest grade, lowest grade, and
calculate the running sum at the same time."

so that threw me off. How would I go about using a loop to read each grade, and distinguishing between each grade in the scanner? So far the way I have it set up it only seems to be reading the first number. Any ideas? Thanks
 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here is what I have now using arrays, I just need to figure out how to use loops instead as instructed. Any Ideas?

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hank Harrison wrote:I just need to figure out how to use loops instead as instructed. Any Ideas?


What loop constructs do you know about and of those which one seems most suitable and why?
 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the "while" loop is what we have been covering in class so I'm assuming that's what the professor is mentioning to put into the code. I don't know how to apply that to each grade individually.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so look at the requirements and identify one place where you may need a loop and decide what determines if the loop should repeat. Then look at your code and find the lines of code that need to be wrapped in a loop. Finally add the while loop.
Now build and test your code and once you have it fully working go back to the requirements and repeat the process for the next loop.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic