我想在n
不使用循环的情况下打印语句次数。
#include<stdio.h>
#include<conio.h>
void show(char *n,int count);
void main()
{
int x=10;
char name[20]="zeeshann";
clrscr();
show (name,10);
getch();
}
void show(char *n,int count)
{
while(count>0)
{
printf("%s\n",n);
count--;
}
}
这是我的代码,其中我使用while循环将字符串打印10次。
如何不使用while
或循环打印10次?
您可以使用递归函数来实现。
递归函数是在执行过程中调用自身的函数。该过程可能重复几次,输出结果和每次迭代的结束。
从show()方法中删除while循环,并使用if条件。
它将持续调用该方法,直到if条件变为false为止,
void show(char *n,int count)
{
if(count>0)
{
printf("%s\n",n);
count--;
show(n,count);
}
}
为了更好的理解,完整代码,
#include<stdio.h>
#include<conio.h>
void show(char *n,int count);
void main()
{
int x=10;
char name[20]="zeeshann";
clrscr();
show (name,10);
getch();
}
void show(char *n,int count)
{
if(count>0)
{
printf("%s\n",n);
count--;
show(n,count);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句