File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Counting down with loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting down with loops" Watch "Counting down with loops" New topic
Author

Counting down with loops

Ian Dudek
Greenhorn

Joined: Feb 03, 2006
Posts: 9
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
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
What do you think num-- does?

How do you think you could change it to do what you want?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
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");
}
}


SCJP 5.0 77%
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

remember that instead of writing "num--" you can write "num = num - 1".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting down with loops