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 5 of 5

Thread: While Loop Help (beginner)

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default While Loop Help (beginner)

    Hello I have an assignment for my beginners java course this semester and I'm having some problems with the program I am trying to create. The purpose of the program is to get a starting number, ending number, and an increment from the user to count upwards from the starting number to the ending number. Also it should say if the number printed is negative or positive which I believe I done correctly in my code using the modulus expression. For example the user chooses 3 for start number and 12 for ending number and 3 for the increment the program should print out:
    The number 3 is negative
    The number 6 is positive
    The number 9 is negative
    The number 12 is positive


    I have some code posted below and It is all kinds of terrible. I was hoping to get a little bit of help.
    import java.util.*;
    public class numbers
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
     
    		int endNumber;
    		int startNumber;
    		int increment;
    		int count;
     
     
    		System.out.print("Your starting number? ");
    		startNumber = console.nextInt();
    		System.out.print("Your ending number? ");
    		endNumber = console.nextInt();
    		System.out.print("Your increment number? ");
    		increment = console.nextInt();
    		System.out.println();
     
    		while(count <= endNumber)
    		{
    			if(0 == count%2)
    			{
    				System.out.println("The number " + count + " is positive");
    			}
    			else
    			{
    				System.out.println("The number " + count + " is negative");
    		    }
    		}
    		System.out.println("Thanks for playing");
    	}
    }


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

    Default Re: While Loop Help (beginner)

    I was hoping to get a little bit of help.
    What sort of help? Does the code compile? Does it behave correctly?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While Loop Help (beginner)

    I cant seem to get it compiled because I don't have count initialized. The problem I seem to be having most is how to actually take the increment the user inputs and get it to count by that number.

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

    Default Re: While Loop Help (beginner)

    I would presume you want to initialize count to the initial number, then increment by increment
    count = startNumber;
    ....
    count += increment;
    ....

  5. The Following User Says Thank You to copeg For This Useful Post:

    Perplexing (October 23rd, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While Loop Help (beginner)

    yes that is exactly what I was wanting. Thank you very much for the help.

Similar Threads

  1. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  2. Programming beginner
    By LatinaC09 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 01:33 PM
  3. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM
  4. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM