下面的代码请求 5 个数字并打印给定数字的星号。数字变量如何记住 5 个数字?输入的下一个数字不会破坏变量内的值吗?我不明白。你能给我解释一下吗?
下面的代码给出了输出:
Enter 5 numbers between 1 and 30: 28 5 13 24 7
****************************
*****
*************
************************
*******
#include <stdio.h>
int main( void ){
int i;
int j; /* inner counter */
int number; /* current number */
printf( "Enter 5 numbers between 1 and 30: " ); /* loop 5 times */
for ( i = 1; i <= 5; i++ ) {
scanf( "%d", &number ); /* print asterisks corresponding to current input */
for ( j = 1; j <= number; j++ )
printf( "*" );
printf( "\n" );
} /* end for */
return 0;
}