我正在处理的程序输出该符号的直角三角形,边长等于该数字。它应该做的是终止,如果你输入0,否则它应该再次要求新的输入。
所以我的问题是如果你输入0我怎么能让它终止,否则要求另一个输入?我知道我可能需要使用while循环。但是我该怎么改呢?
这是我的代码:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s; /*s is symbol (the input)*/
int a, b, n; /*n is number of rows (the input)*/
printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol input*/
scanf_s("%c", &s, 1);
printf("Please type a positive non-zero number between 5 and 35:...\n"); /*Ask for number of rows input*/
scanf_s("%d", &n);
assert(n >= 5 && n <= 35);
for (a = 1; a <= n; a++) /*How many rows to display+create*/
{
for (b = 1; b <= a; b++)
{
printf("%c", s);
}
printf("\n");
}
system("PAUSE");
}
答案 0 :(得分:0)
你可以使用循环来做到这一点。
#include <stdio.h>
#include <stdlib.h>
#ifndef _MSC_VER
/* passing extra arguments to scanf is not harmful */
#define scanf_s scanf
#endif
int main(void)
{
char s; /*s is symbol (the input)*/
int a, b, n; /*n is number of rows (the input)*/
do {
printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol input*/
scanf_s("%c", &s, 1);
printf("Please type a positive non-zero number between 5 and 35:...\n"); /*Ask for number of rows input*/
scanf_s("%d", &n);
if(n >= 5 && n <= 35)
{
for (a = 1; a <= n; a++)/*How many rows to display+create*/
{
for (b = 1; b <= a; b++)
{
printf("%c", s);
}
printf("\n");
}
}
while ((a = getchar()) != '\n' && a != EOF); /* remove the newline character from standard input buffer */
} while (n != 0);
system("PAUSE");
}