如果“across”或“down”小于或等于零,我正在尝试写if语句以不绘制任何内容。不太明白如何写它来停止draw_rect。虽然我已经放下了= 0
,但仍然有问题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;
}
答案 0 :(得分: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 );
}
}
反过来想想它。