在C语言中,可以使用不同的循环结构来实现重复语句。以下是几种常见的方法:
while循环
```c
include int main() { int i; char c; printf("Enter a character to repeat: "); scanf("%c", &c); printf("How many times do you want to repeat it? "); int n; scanf("%d", &n); while (n > 0) { printf("%c", c); n--; } printf("\n"); return 0; } ``` ```c include int main() { int i; char c; printf("Enter a character to repeat: "); scanf("%c", &c); printf("How many times do you want to repeat it? "); int n; scanf("%d", &n); for (i = 0; i < n; i++) { printf("%c", c); } printf("\n"); return 0; } ``` ```c include int main() { int i; char c; printf("Enter a character to repeat: "); scanf("%c", &c); printf("How many times do you want to repeat it? "); int n; scanf("%d", &n); do { printf("%c", c); n--; } while (n > 0); printf("\n"); return 0; } ``` 这些示例程序都实现了用户输入一个字符并指定重复次数,然后程序会输出指定数量的相同字符。你可以根据需要选择合适的循环结构来实现重复执行同一程序。for循环
do-while循环