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

Thread: Basic while loop issues

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic while loop issues

    In my first program I was asked to output the perfect squares less than the value n.
    	int i = 0;
    	int x = 0;
    	int n = in.nextInt();
    		while (i < n)
    		{
    			i = x*x;
    			System.out.print(i + " ");
    			x = (x + 1);
    		}

    My issue is if I enter 90, it outputs 100. I know this is because after 9*9 it goes to 10*10 but how can I change it so it doesn't do this?

    My second issue is a bit more complicated and I have no idea what to do. I have to output all positive numbers divisible by 10 less than n:

    	int i = 0;
    	int x = 0;
    	int n = in.nextInt();
    		while (i < n && i > 0)
    		{
    			i = x/n;
    				if (i%10==0)
    				{
    					System.out.print(i + " ");
    				}	
    				else 
    				{
    					x = (n-1);
    				}
    		}

    my output is all zeros. Why?! How do I fix this?
    Last edited by Litost; October 8th, 2012 at 12:55 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic while loop issues

    my output is all zeros. Why?
    int x = 0;

    Also: i = x/n;
    if x < n because of integer math


    Try debugging the code by adding some println statements to print out the values of all the variables as they are set and used.
    Last edited by Norm; October 8th, 2012 at 07:18 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Using recursion issues for basic program
    By ash12 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 5th, 2012, 10:40 PM
  2. BASIC: I don't even know how to begin to loop this.
    By adamglennschwartz in forum Loops & Control Statements
    Replies: 1
    Last Post: October 18th, 2011, 07:40 PM
  3. [SOLVED] VERY basic question - if loop
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 12th, 2011, 06:34 PM
  4. Basic treemap loop
    By Trunk Monkeey in forum Java Theory & Questions
    Replies: 3
    Last Post: May 1st, 2011, 10:11 AM
  5. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 05:10 PM