1

So the problem is the following :

"Write a method for the Rectangle class called draw that draws a rectangle using dashes and vertical bar characters. The following code sequence

Rectangle *myRect = [[Rectangle alloc]init];
[myRect setWidth : 10 andHeight : 3];
[myRect draw];

Would produce the following output : "

(i cant show you the picture but its a rectangle made out of "-" dashes and "|" bar characters. Dashes are for width and bar characters for height.)

I've started doing the method like this :

{
int n;

for ( n = 1 ; n <= self.width ; ++n)
    printf ("-");

for ( n = 1 ; n <= self.height ; ++n){
    printf ("\n|");
        }
    printf("\n");

for ( n = 1 ; n <= self.width ; ++n){
    printf ("-");
}

But it seems like its not going to work like that , i cant get it to display the outer (|) lines . Could anyone help me on this one?

4

3 に答える 3

0

あなたはObjective-Cで作業していると思いますが、Cでテストした簡単なコードセットを次に示します。これは非常に簡単に変換できます。

void printRectangle(int width, int height)
{
     int n;
     int z;

     printf(" ");

     for (n = 1 ; n <= width ; n++)
        printf ("-");

     printf("\n");

     for ( n = 1 ; n <= height ; n++)
     {
        printf ("|");

        for(z = 1; z <= width; z++)
             printf(" ");

        printf("|\n");
     }

     printf(" ");

     for ( n = 1 ; n <= width ; ++n)
         printf ("-");

     printf("\n");
}

幅 = 10、高さ = 5 の出力

 ----------
|          |
|          |
|          |
|          |
|          |
 ----------
于 2013-02-09T21:06:19.413 に答える
0

申し訳ありませんが、上記の受け入れられた回答が表示されませんでした。ただし、念のためこれは残しておきます。

私はObjective Cでテストしていませんが、試してみてください:)

for (int y = 0; y <= self.width; y++) {
    for (int x = 0; x <= self.width; x++) {
        if (x == 0 || y == 0 || x == self.width || y == self.height) {
            if (y == 0 || y == self.height) {
                printf("-");
            }
            else if (x == 0 || x == self.width) {
                printf("|");
            }
            if (x == 10) {
                printf("\n");
            }
        } else {
            printf(" ");
        }
    }
  }

これはJavaで書かれており、あなたのものに合わせていくつかの単語を変更しただけです. :)

出力:

-----------
|         |
|         |
|         |
|         |
|         |
|         |
-----------
于 2013-02-09T21:07:22.817 に答える
0
//  Created by Victor Diaz  on 11/10/13.
//  Copyright (c) 2013 Victor Diaz . All rights reserved.
//  lines of code are submitted but  i have just inputed here the function draw

-(void)draw{
    int i=0;
    printf(" ");
    for(i=0; i<=width;i++){
        printf("-");
    }
    printf("\n");
    for(i=0;i<=height;i++){
        printf("|");
        for (int z=0; z<width; z++)
            printf(" ");
            printf(" |\n");
    }
    printf(" ");
    for (i=0; i<=width; i++) {
        printf("-");
    }
    printf("\n");
}
@end
于 2013-11-17T16:19:23.047 に答える