• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if statement help

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

i'm having a problem with this program not working as I intended.

So the program works, too well actually, when the seconds entered go past 3600 or 86400 the program calculates the mins, hours and seconds. I am trying to make it so if the seconds are 90000 it will just give me the days. or if its 7000 seconds then it will calculate the hours.

thank you

 
Greenhorn
Posts: 6
C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Martinez wrote:



Try this intead of the above code snippet:
 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you that worked, and it helped me understand what I was doing incorrectly.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nimit Bhargava wrote: . . .

Although that code accurately reflects its structure, you should add {} and it is usual to indent else‑ifs differently, so it comes out as:-Whenever you use a block of else‑ifs, make sure they run in order.
More style things: don't expect people to know what 60 or 86400 mean: use constants: SECONDS_IN_DAY or SECONDS_IN_MINUTES. And never use the tab key.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I most cases where you use multiple if‑elses, it is worth ending the series with a plain simple else to catch any possibilities not yet covered. In some cases the code won't compile otherwise.
 
Nimit Bhargava
Greenhorn
Posts: 6
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion, Campbell.
Will follow that from now.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. I've even written something like


Then if it DOES happen, I know I have some logic wrong somewhere...
 
reply
    Bookmark Topic Watch Topic
  • New Topic