• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Determining Adult Problem

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys
working on program which has to determine
1.the average of the age of input people
2. how many are adult among them and write the number of adults only (example if I have 15 16 24 and 26..program has to write only 2 adults
now with first part, no problem
second part is the problem

how do I get the program to write the amount of people over 18 years
thanks

 
author & internet detective
Posts: 41407
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: you need another variable and an if statement to keep track of the number of adults.
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's strange, I don't feel adult at all
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys, I know I need an "if" but where do I put that
 
Jeanne Boyarsky
author & internet detective
Posts: 41407
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside the for loop.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to check age of each student to determine if he/she is adult or not. Each time you encounter an adult you will increase the counter. As you are reading inputs in loop so you will
1. Have to check if age meets with adult criteria (if condition)
2. Have to increase the counter when age meets the condition(counter++) otherwise do nothing.

and off course all this will happen inside the loop unless you want to store ages of all students somewhere, which is not required.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys thanks
after banging my head for a while and your help I managed to get something right but now I have another problem

I can't seem to get the average of ages right
I know it has to do with the " age+=" on line 14 but I can't get that part right
If I leave the " age+=" my number of adults count is screwed but average age is fine
if I take it out " age=" my number of adults is good but then my average is screwed
how to go about this
thanks


 
Saloon Keeper
Posts: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need two different variables: age, and sumOfAges.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks carey ..was not thinking about that one.
Thanks to everybody else for the invaluable help
 
today's feeble attempt to support the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic