CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2017
    Posts
    3

    Incorrect output when printing diamond using do while loop

    I am creating code to print a diamond using only Do while loops and I have got the following code:

    Code:
    public static void diamond3() {
        System.out.println("Output for: Do while Loop");
        
        int noOfRows = DIAMOND_SIZE;
        
        //Getting midRow of the diamond
        int midRow = (noOfRows)/2;
        
        //Initializing row with 1
        int row = 1;
        
        int i = midRow;
        
        do {
            //Printing i spaces at the beginning of each row
            int j = 1;
            do {
                System.out.print(" ");
                j++;
        }
            while (j <= i);
        
        
            //Printing j *'s at the end of each row
            j = 1;
            do {
                System.out.print("* ");
                j++;
            }
            while (j <= row);
            System.out.println();
        
            //Incrementing the row
            row++;
        
            i--;
        }  
        while (i > 0);
        
        i = 0;
        
        do { 
            //Printing i spaces at the beginning of each row
            int j = 1;
            do {
                System.out.print(" ");
                j++;
            }
            while (j <= i);
            
            //Printing j *'s at the end of each row
            int mid = (row+1)/2;
            j = row;
            do {
                if(i==0 && j==mid) 
                    System.out.print("o ");
                
                else 
                    System.out.print("* ");
                    j--;
            }
            while (j > 0);
            System.out.println();
        
            //Decrementing the row
            row--;
            i++;
        }
        
        while(i <= midRow);
        }
    Im meant to get a diamond shape but I am getting the following shape:

    Code:
      
          * 
         * * 
         * o * 
         * * 
          *
    The expected out is:

    Code:
             * 
          * * * 
       * * o * *
          * * * 
             *
    Can someone help figure out where I have gone wrong. I have checked the code so many times but unable to figure out where the mistake is.

  2. #2
    Join Date
    Feb 2017
    Posts
    3

    Re: Incorrect output when printing diamond using do while loop

    The expected outcome is meant to be a diamond like
    *
    * * *
    * * o * *
    * * *
    *

  3. #3
    Join Date
    Feb 2017
    Posts
    3

    Re: Incorrect output when printing diamond using do while loop

    Code:
         * 
      * * * 
    * * o * *
      * * * 
        *

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Incorrect output when printing diamond using do while loop

    Can you describe what is wrong with the output? Where in the code is the line with the bad content printed?

    Also posted at: http://www.dreamincode.net/forums/to...and-o-in-java/
    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured