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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Displaying increments of 0.5

  1. #1
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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);
    			}
    		}
    	}
    Last edited by helloworld922; September 27th, 2011 at 03:59 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Displaying increments of 0.5

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

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

Similar Threads

  1. [SOLVED] Need help with displaying items with my program
    By mike101290 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 08:07 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  4. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM
  5. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM