该符号的直角三角形,边长等于该数字 [英] Right angle triangle of that symbol with sides equal to that number

查看:101
本文介绍了该符号的直角三角形,边长等于该数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我对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);
}

解决方案

问问自己..



while()语句的格式是什么和

while的结束/测试条件是什么?和

我需要重复的代码是什么



所以你可能会想出

 (某些测试条件)
{
// 做一些有用的事情
}




第一个
,是吗?



你在这里'n'



 scanf_s( %d,& n); 





所以也许'在下面'你需要



 (n!=  0 
{
// 做一些有用的东西
}





当然有一个问题 - 除非你得到'n'再次,在你做了'有用的东西'后,你永远不会打破'while'循环(你会崩溃)..所以也许你需要在底部重复一些代码那时...



<前一个=c ++> (n!= 0
{
// 做有用的东西

// 再次获取
printf( 请输入正数非零数字5..35或0以退出\ n);
scanf_s( %d,& n);
}





只是让你填写'Do Something useful'。我会考虑移动在那里'for loop'个人



是吗?代码我已经证明你'不是很好' - 你可以做得更好,而不是重复printf和scanf_s例如..现在由你填写'// Do Something Useful'位...保存副本您的程序在进行更改之前,如果需要,可以返回


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



如果你不明白为什么你的程序做了什么或不做什么,调试器将允许你逐行运行并明白为什么你有意想不到的ha行。

引用:

WOO !!谢谢!我知道了!现在我的程序能够重启。但是,我想知道是否有办法再次要求输入符号?当我添加



 printf( 请输入三角形的符号:... \ n); 
scanf_s( %c,& s, 1 );





之前



 printf( 请输入一个正的非零数字5..35或0到quit\\\
);
scanf_s( %d,& n);





,程序有问题。 :(



你的程序可能有一些键盘缓冲区,第一个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");
}

解决方案

ask yourself ..

"what is the format of a while() statement" and
"what is the end/test condition for the while" and
"what code do I need to repeat"

so you might come up with

while (some test condition)
{
    // Do Something useful
}



for the first one, yes ?

you're getting 'n' here

scanf_s("%d", &n);



so maybe 'underneath that' you need

while (n != 0)
{
//  Do Something useful
}



there's a catch of course - unless you get 'n' again, after you've done 'something useful', you'll never break 'out of' the while loop (and you'll get a crash) .. so maybe you need to repeat some code at the bottom of the while ...

while (n != 0)
{
    // Do Something useful

    // Get n again
    printf("Please type a positive non-zero number 5..35 or 0 to quit\n");
    scanf_s("%d", &n);
}



which just leaves you to fill in 'Do Something useful' .. I'd be looking at moving the 'for loop' in there personally

yes ? the code Ive shown you 'isnt great' - you could make it better, instead of repeating the printf and scanf_s for example .. Its up to you to fill in the '// Do Something Useful' bit now .. save a copy of your program before you make the changes, so you can go back to it if you need to


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

When you don't understand why your program do something or don't, the debugger will allow you to run line by line and see why you have ab unexpected behavior.

Quote:

WOO!! Thanks! I got it! Now my program is able to "restart". However, I am wondering if there is a way to ask for symbol input again? When I add

printf("Please type the symbol of the triangle:...\n");
scanf_s("%c", &s, 1);



before

printf("Please type a positive non-zero number 5..35 or 0 to quit\n");
scanf_s("%d", &n);



,the program has something wrong. :(


Your program have probably something in keyboard buffer and the first scanf just eat it.


这篇关于该符号的直角三角形,边长等于该数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆