gpt4 book ai didi

c - 在 C 中使用 while 循环每行少打印一个 "*"

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:20 25 4
gpt4 key购买 nike

在用户使用 while 循环指定初始数字后,每行少打印一个星号。

这是一个学校项目,所以我不是要你回答,只是需要指出正确的方向。

int main() {
int number;
int stars;
char st = '*'; //using char to setup the "*"

printf("please enter a positive number:");
scanf("%d", &stars);

while (stars > 0) {
stars = (char) st; //tryin to convert number eneter to a char
stars = stars - 1;
printf("%c \n", stars);
}

return 0;
}

我迷路了,我应该将整数转换为字符还是?

最佳答案

请按照以下步骤操作:

  • 从用户那里读取星星的数量(你几乎是正确的,你应该检查 scanf() 返回 1。

  • 使用 for 循环并递减 star 并在 0 处停止。

  • 在循环中,编写另一个循环来打印 *star 副本和换行符

  • 就是这样!返回 0。

关于c - 在 C 中使用 while 循环每行少打印一个 "*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40621581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com