Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java For Loops

Vladimir Grbic
Vladimir Grbic
324 Points

Just curious...

What if I wanted to create a timer that counts down the seconds.

for (int i = 60; i > 0; i--) { console.printf("%d seconds left. %n", i); }

But when: "i" gets decremented to 1 it is going to say "1 SECONDS left", which is grammatically incorrect. Also when it gets to 0 I want it to say "Time has ran out"

I know I can do this using a conditional I JUST DON'T KNOW HOW TO IMPLEMENT THAT CONDITIONAL IN FOR LOOP.

Thanks!

3 Answers

Brandon Khan
Brandon Khan
21,619 Points

Well i guess what i was saying is i'm learning too just like you and from my limited exp i see there are a lot of ways to do the same thing.

Brandon Khan
Brandon Khan
21,619 Points

you can add if conditions into the for loop. I'm sure there is a more effective way but try this....

if (i == 1){ console.printf(""%d seconds left. %n", i"); } if(i == 0){ console.printf(""Time has ran out"); }

MODERATOR EDIT: Moved from Comments to Answers.

Vladimir Grbic
Vladimir Grbic
324 Points

Wow. That was simple...

Since you mentioned that there is more practical way, can you please explain it?