• 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

Counting down with loops

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I have been working on loops and I need to count down from 50 to 0 but by counting down by 5s. So 50 45 40 ect. Now I have to use continue or break in this program. I have been fooling around with it but still have no clue on how to have it only display 50 45 40 ect.

This is what I have so far
public class Lab12B {
public static void main(String[] args) {
for (int num = 50; num >= 0; num--) {
if (num == 0)
continue;
else

System.out.println(num);
}
System.out.println("DONE");
}
}

I keep on wanting to put in num%5 ==0 but all that does is skip every 5.

Well as always thanks in advance
Ian
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think num-- does?

How do you think you could change it to do what you want?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you use if ( i==0 ) continue; ?

Simply think :
I need a counter starting at 50, decrementing it by 5 until it becomes negative. You should be able to translate this into a for loop.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works fine

class Lab12B
{
public static void main(String[] args)
{
for (int num = 50; num >= 0; num--) {
if ((num%5) == 0)
System.out.println(num);
}
System.out.println("DONE");
}
}
 
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
remember that instead of writing "num--" you can write "num = num - 1".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic