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?
Code Java:
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);
}
}
}
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?
Re: Displaying increments of 0.5
so the for loop will need to be for (double i=0;i<=25;i++)? :)
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)