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

Thread: Do-while loop help

  1. #1
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Do-while loop help

    What I am trying to accomplish is having the user input a number (5 for example), and then printing 25, 16, 9, 4, and then 1 (squaring each number until it hits 0).

    Here is the question: U9IwaIygBD.jpg

    Here is my code so far:

    import java.io.*;
    import java.util.*;
     
    public class Problem
    {
    	public static void main(String args[])
    	{
     
    		System.out.print("\nPlease enter an integer. ");						
    		Scanner b=new Scanner(System.in);	
    		int g = b.nextInt(), sum = 0, r = 0, f = g;												
     
    		do
    		{
     
    			sum = g * g;
    			g*=g;	
     
    		System.out.println("\n" + sum);
     
    			f--;
    			sum = f * f;
     
    		System.out.println("\n" + sum);
     
    		}
    		while(g<0);
    }
    }

    This prints 2 squared numbers. So if I entered 5, 25 and 4 would print. Obviously I want to make it so I don't need to keep making system.out.printlns, and just have a very small block of code. If you can help that would be greatly appreciated.
    Last edited by phlegm; November 7th, 2013 at 10:15 PM. Reason: problem solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Do-while loop help

    make it so I don't need to keep making system.out.printlns
    Can you explain? Are you asking how to only call println() one time and have it print all the results?
    If that is want you mean, you could use String concatenation to build a String will all the results and then call println() one time to print that String.

    int g = b.nextInt(), sum = 0, r = 0, f = g;
    Putting multiple statements one on line tends to hide what the code is doing.
    It would be better to split that line into several lines.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do-while loop help

    Quote Originally Posted by Norm View Post
    Can you explain? Are you asking how to only call println() one time and have it print all the results?
    If that is want you mean, you could use String concatenation to build a String will all the results and then call println() one time to print that String.

    int g = b.nextInt(), sum = 0, r = 0, f = g;
    Putting multiple statements one on line tends to hide what the code is doing.
    It would be better to split that line into several lines.
    What I'm trying to do is to make only one System.out.println that will print every iteration. For example, if I enter in "6" into the command prompt, I would like this to be printed.

    36
    25
    16
    9
    4
    1

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Do-while loop help

    What is f for? Would using just g work?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do-while loop help

    Quote Originally Posted by Norm View Post
    What is f for? Would using just g work?
    Yes I believe it would, but I don't know how to use g twice in the loop. :/

    Using the method I'm using, I would have a lot of code, so I realized I couldn't keep doing it the way I am.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Do-while loop help

    what if only f were used and the f-- statement was moved?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do-while loop help

    Quote Originally Posted by Norm View Post
    what if only f were used and the f-- statement was moved?
    I honestly don't know how I would set that up. sorry

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Do-while loop help

    Just think about the problem a bit more.
    You are wanting to start at a given number, square it and print it out, then subtract one from it.

    So just some example of pseudocode:

    Create variable for storing the input number
    Create variable for storing sum

    Loop while input number is greater than 0
    Assign sum to be the input squared
    Print out sum
    Decrement the input number
    Continue loop

    Hopefully this helps a little bit without giving too much away

  9. #9
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do-while loop help

    Quote Originally Posted by Parranoia View Post
    Just think about the problem a bit more.
    You are wanting to start at a given number, square it and print it out, then subtract one from it.

    So just some example of pseudocode:

    Create variable for storing the input number
    Create variable for storing sum

    Loop while input number is greater than 0
    Assign sum to be the input squared
    Print out sum
    Decrement the input number
    Continue loop

    Hopefully this helps a little bit without giving too much away
    This does help a lot, but do I create these variables in or out of the loop? Or does it not matter?

    --- Update ---

    Ok, so I think I improved my code. Thanks @Parranoia.

    System.out.print("\nPlease enter an integer. ");						
    		Scanner b=new Scanner(System.in);	
    		int g = b.nextInt(), sum = 0;											
     
    		do
    		{
    			sum = g * g;
    		System.out.println("\n" + sum);
    			g--;	
    		}
    		while(g<0);

    The only thing is, is that I don't know how to continue my loop. If I entered in "5" right now with this code, it only prints out 25.

  10. #10
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Do-while loop help

    Variables created inside a loop will only exist while that loop is running. Take this as an example:
    int num = 0;
     
    for (int i = 0; i < 5; i++)
    {
        num = i * i;
        int sum = 2;
    }
     
    System.out.println(num);
    System.out.println(sum); // This will not compile as it is not defined in this scope

    If you need a variable to persist after a loop has completed, you must define it outside of the loop.
    At the end of each loop the variable sum would get destroyed then created again at the start of the next loop.

    In this example, you can access the variable num even after the loop has completed, but not sum.

    --- Update ---

    Quote Originally Posted by phlegm View Post
    This does help a lot, but do I create these variables in or out of the loop? Or does it not matter?

    --- Update ---

    Ok, so I think I improved my code. Thanks @Parranoia.

    System.out.print("\nPlease enter an integer. ");						
    		Scanner b=new Scanner(System.in);	
    		int g = b.nextInt(), sum = 0;											
     
    		do
    		{
    			sum = g * g;
    		System.out.println("\n" + sum);
    			g--;	
    		}
    		while(g<0);

    The only thing is, is that I don't know how to continue my loop. If I entered in "5" right now with this code, it only prints out 25.
    The logic of your do-while loop is: do such and such while g is less than 0

  11. The Following User Says Thank You to Parranoia For This Useful Post:

    phlegm (November 7th, 2013)

  12. #11
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do-while loop help

    I figured it out, thanks everyone (Parranoia, and Norm).

    It was a very foolish error. The control statement was (g<0), when it should of been (g>0). I feel dumb. But thanks!

  13. #12
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Do-while loop help

    Everybody has been there buddy it'll only get better and better with time. Glad to help out.

  14. The Following User Says Thank You to Parranoia For This Useful Post:

    phlegm (November 7th, 2013)

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM