0

I am trying to find a code that creates a box such as... XXX XXX XXX (exp. 3x3) depending on what the user inputs as row and height variables. My nested loop is not working correctly. Can I please get some help?

Heres code...

Set ServerOutput on;
Declare

  RectangleHeight Number(2):=5;
  RectangleWidth Number(2):=;
  RowCount Number(1) :=0;
  ColumnCount Number(1) :=0;
 Begin 
Loop 
Exit When RowCount>=RectangleHeight;
    Loop
    Exit When ColumnCount>=RectangleWidth;
    dbms_output.put_line('X');  
    ColumnCount := ColumnCount+1;
    End Loop;
RowCount := RowCount+1;
End Loop;


End;
Nick
  • 1,417
  • 1
  • 14
  • 21
Luke
  • 31
  • 8

1 Answers1

0

Nothings really wrong with the loop as much as the logic needs tweaked a bit.
First thing you have a syntax error on line 5: RectangleWidth Number(2):=;
Fixing that we have a few more problems to figure out.

1) You are putting a new line \n after each X in the width portion of the loop. This should probably be a single X without the new line.
2) After you output the object width, what happens to the ColumnCount variable?
3) After you output an entire row, how do you get to a new line?

Example code with comments:

SET ServerOutput ON;
DECLARE
  RectangleHeight NUMBER(2) :=5;
  RectangleWidth  NUMBER(2) :=9; -- FIXED SYNTAX ERROR
  RowCount        NUMBER(1) :=0;
  ColumnCount     NUMBER(1) :=0;
BEGIN
  LOOP
    EXIT WHEN RowCount>=RectangleHeight;
    LOOP
      EXIT WHEN ColumnCount>=RectangleWidth;
        dbms_output.put('X'); -- DON'T PUT A NEW LINE, JUST USE PUT
        ColumnCount := ColumnCount+1;
    END LOOP;
    ColumnCount := 0; -- AFTER A SUCCESSFUL ROW IS OUTPUT, WE RESET OUR COLUMN COUNTER
    RowCount := RowCount+1;
    dbms_output.put_line(null); -- JUST OUTPUTTING A BLANK LINE
  END LOOP;
END;
/

And the output using 5 and 9 as inputs for height and width respectively:

XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX

I feel like this is homework but I always liked these kind of visual problems.

mmmmmpie
  • 2,908
  • 1
  • 18
  • 26
  • Yep this is for homework and it does exactly what I want it to do. Thank you very much!!! – Luke Mar 06 '15 at 18:18