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

Thread: Can someone tell me how to fix this please

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Can someone tell me how to fix this please

    im practicing how to do the do while loop in java and i cannot figure out how to lmake it start with zero instead of one so that the output would be o is o doubled and then quadrupled instedead of it saying number 1 and so forth.
    Can someone please tell me how to do that?
    here is the code i have written so far.

    // NewestDoubleQuadruple.java - This program prints the numbers 0 
     
    through 10 along
    // with their values doubled and quadrupled.
    // Input:  None.
    // Output: Prints the numbers 0 through 10 along with their 
     
    doubles and quadruples.
     
     
    public class NewestDoubleQuadruple
    {
    	public static void main(String args[])
    	{
     
    		String head1 = "Number: ";
    		String head2 = "Doubled: ";
    		String head3 = "Quadrupled:  ";				
    		int numberCounter; 	// Number 0 through 10.
    		int doubled;		// Stores the double of a 
     
    number.
    		int quadrupled;		// Stores the quadruple of 
     
    a number.
    		final int NUM_LOOPS = 10; // Constant used to 
     
    control loop. 
     
    		// This is the work done in the housekeeping() 
     
    method
    		System.out.println("Doubles and Quadruples" + 
     
    "\n");
     
    		// This is the work done in the detailLoop() 
     
    method
    		numberCounter = 0;
    		// Write your do while loop here
    	do
    	{
     
     
    	numberCounter++;
    	doubled=numberCounter*2;
    	quadrupled=numberCounter*4;
     
    	System.out.println(head1 + numberCounter);
    		System.out.println(head2 + doubled);
    		System.out.println(head3 + quadrupled);
     
     
    }
    while(numberCounter<NUM_LOOPS);
     
                    // This is the work done in the EndOfJob() method	
    		System.exit(0);
    	} // End of main() method.
     
    } // End of NewestDoubleQuadruple class.
    Last edited by helloworld922; October 5th, 2011 at 01:47 AM.


  2. #2
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone tell me how to fix this please

    Put the line:

    numberCounter++;

    to the end of do's body section.

    java memory
    Last edited by namhm; December 4th, 2011 at 06:55 PM.

  3. #3
    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: Can someone tell me how to fix this please

    Quote Originally Posted by namhm View Post
    Put the line:

    numberCounter++;

    to the end of do's body section.
    Instead of simply telling him what to do, why don't you explain why that solves the problem? I appreciate you're trying to help, but simply posting code isn't helpful by itself. It's best to explain the "why" of the situation, not just the "what".
    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!

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone tell me how to fix this please

    Quote Originally Posted by KevinWorkman View Post
    Instead of simply telling him what to do, why don't you explain why that solves the problem? I appreciate you're trying to help, but simply posting code isn't helpful by itself. It's best to explain the "why" of the situation, not just the "what".
    Okay, guy, I was thinking that it is pretty simple for him to understand.
    I explain it here: because the value of the variable numberCounter is increased by one before the System.out.println() gets called, so if you want to make it starts with zero, put the line numberCounter++ at the end of the do-while's body.

    java memory
    Last edited by namhm; December 4th, 2011 at 06:55 PM.

  5. #5
    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: Can someone tell me how to fix this please

    Quote Originally Posted by namhm View Post
    Okay, guy, I was thinking that it is pretty simple for him to understand.
    It might be simple to understand, but it's more helpful to supply the "why" of the situation than it is to supply "just do this". Especially for newbies, it can be pretty hard to connect the code to what the program does, and oftentimes just a few sentences explaining what's going on can go a lot further than a line of code.
    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!

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone tell me how to fix this please

    Quote Originally Posted by KevinWorkman View Post
    It might be simple to understand, but it's more helpful to supply the "why" of the situation than it is to supply "just do this". Especially for newbies, it can be pretty hard to connect the code to what the program does, and oftentimes just a few sentences explaining what's going on can go a lot further than a line of code.
    Thanks Kevin, I will update the way of my answer in subsequent posts.