2016-08-12 55 views
-1

我試圖寫if語句,如果「across」或「down」小於或等於零時不繪製任何內容。不太明白如何寫它來停止draw_rect。有問題,因爲它仍然即使我已經放下= 0C#if語句不做某事

int horiz; 
int vert; 
int across; 
int down; 

char display_char; 

void draw_rect (void) { 

    //  If either of the width or height is less than or equal to zero, 
    //  the function must not draw anything. 


    draw_line(horiz, vert, across, vert, display_char); 
    draw_line(horiz, down, across, down, display_char); 
    draw_line(horiz, vert, horiz, down, display_char); 
    draw_line(across, vert, across, down, display_char); 

    if (down > 0){ 
     ????? 
    } 

int main(void) { 
    setup_screen(); 

    // draw a box. 
    horiz = rand() % screen_width()/2; 
    vert = rand() % screen_height()/2; 
    across = 1 + rand() % (screen_width() - horiz - 1); 
    down = 1 + rand() % (screen_height() - vert - 1); 
    display_char = '@'; 
    draw_rect(); 
    show_screen(); 

    // draw a box with zero height. 
    horiz = rand() % screen_width()/2; 
    vert = rand() % screen_height()/2; 
    across = 1 + rand() % (screen_width() - horiz - 1); 
    down = 0; 
    display_char = 'a'; 
    draw_rect(); 
    show_screen(); 




    timer_pause(5000); 
    cleanup_screen(); 
    return 0; 
} 
+1

把if放在你的畫線之前。因此'if(YourConditionMeets)然後繪製;'並且想到使用['DrawingContext.DrawRectangle方法'](https://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.drawrectangle(v = vs.110).aspx)無論如何 –

+0

還記得爲'draw_rect'添加一個右大括號。 – naffarn

回答

2

說起來很簡單得出:

void draw_rect (void) 
{ 

//If either of the width or height is less than or equal to zero, 
//the function must not draw anything. 

    if(across > 0 && down > 0) 
    { 
     draw_line(horiz, vert, across, vert, display_char); 
     draw_line(horiz, down, across, down, display_char); 
     draw_line(horiz, vert, horiz, down, display_char); 
     draw_line(across, vert, across, down, display_char); 
    } 
} 

想想它周圍的其他方法。