Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Displaying increments of 0.5

1. ## Displaying increments of 0.5

I started learning Java about a week ago and I'm doing some loops now I wrote a class that counts up to 25 and displays the numbers. I changed a few things here and there to see what would happen now I want to display the numbers in .5 increments. Here is my code and I'm not sure quite what to do, is there a tutorial i can do to learn how?
```public class NewLoopTest
{
public static void main (String[] args)
{
for (int i=0;i<=25;i++)
{
if ((i%.5)== 0)
//((i%5)!= 0)when i is divisible by 5 the number will not be shown
//((i%5)== 0)all numbers divisible by 5 will be shown
//((i%5)== 1)count will start at 1 and add 5 @ each increment, these will be shown
//((i%5)== .5) ??
{
System.out.println(i);
}
}
}```

2. ## Re: Displaying increments of 0.5

Why don't you just use a double and increment by .5 instead of using an int and incrementing by 1?

3. ## Re: Displaying increments of 0.5

so the for loop will need to be for (double i=0;i<=25;i++)?

4. ## Re: Displaying increments of 0.5

If you have not yet, I recommend reading the following:
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Note the definition of a for loop...while quite often they are written as integer based and increments are by an integer value, this is not a requirement. Kevin's suggestion was to increment the looping variable by 0.5 (in other words, i += 0.5)

5. ## The Following 2 Users Say Thank You to copeg For This Useful Post:

KevinWorkman (September 27th, 2011), PsYNus (September 28th, 2011)