问题描述
您好。我对C语言有疑问。正在进行的程序是一个程序,它输出该符号的直角三角形,边长等于该数字。如果输入0,程序将终止,否则将再次请求新输入。这是我的代码:
所以我的问题是如果输入0,如何终止否则它会再次要求新输入。
我知道我可能需要使用while循环。但是我该怎么改呢?
我尝试过的事情:
#include < assert.h >
#include < stdio.h >
#include < stdlib.h >
int m ain()
{
char s; / * s是符号(输入)* /
int a,b,n; / * n是行数(输入)* /
printf ( 请输入三角形的符号:... \ n); / * 请求符号输入* /
scanf_s( %c,& s, 1 );
printf( 请输入5到35之间的正非零数字:... \ n 跨度>); / * 询问输入的行数* /
scanf_s( %d,& n);
断言(n> = 5 && n< = 35 );
for (a = 1 ; a< = n; a ++) / * 要显示的行数+创建* /
{
for (b = 1 ; b< = a; b ++)
{
printf( %c,s);
}
printf( \ n);
}
system( PAUSE);
}
你的程序可能有一些键盘缓冲区,第一个scanf只吃它。
Hello. I have a question on C language. The program im working on is a program that outputs a right angle triangle of that symbol with sides equal to that number. Program terminates if you enter 0 otherwise it will ask for a new input again. Here is my code:
So my question is that "HOW to terminates if you enter 0 otherwise it will ask for a new input again. "
I know I probably need to use while loop. But how do i change it?
What I have tried:
#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"); }
Your program have probably something in keyboard buffer and the first scanf just eat it.
这篇关于该符号的直角三角形,边长等于该数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!