这样我就完成了我的代码,它提供了所需的输出,但是它不会终止!我该如何解决?
我尝试退货,但找不到合适的存放地点
这样我就完成了我的代码,它提供了所需的输出,但是它不会终止!我该如何解决?
我尝试退货,但找不到合适的存放地点
#include<stdio.h>
void get_input();
void is_valid(int num);
void print_pattern(int num);
void main ()
{
get_input();
}
void get_input()
{
int a;
printf("Enter an odd number less than or equal to 9 and greater than 0 > ");
scanf("%d",&a);
is_valid(a);
}
void is_valid(int num)
{
if(num > 9)
printf("You have entered a number greater than 9. Please try again.\n\n");
else if (num < 1)
printf("You have entered a number less than 1. Please try again.\n\n");
else if (num % 2 == 0)
printf("You have entered an even number. Please try again\n\n");
else if (num >= 1 && num <= 9 && num%2!=0)
print_pattern(num);
get_input();
}
void print_pattern(int num)
{
int j,k,l;
for ( j = 0; j<num/2 ; j++ ){
for ( k = 0; k<num/2 - j; k++){
printf(" ");
}
for( l=0; l < (2*j + 1); l++){
printf("%2d",l+1);
}
printf("\n");
}
for( l=0; l <num; l++) // to print middle
{
printf("%2d",l+1);
}
printf("\n");
// to print bottom
for ( j = 0; j<num/2 ; j++){
for ( k = 0; k<j+1 ; k++){
printf(" ");
}
for ( l=1; l< num - (2*j + 1); l++){
printf("%2d",l);
}
printf("\n");
}
}
最佳答案
无休止的循环main
调用get_input
,该is_valid
调用get_input
,该if (num < 0) return
在此处进行间接递归。
如果要终止,请为num输入一些if条件,例如
关于c - 如何在C中终止程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42351083/