c语言程序如何让其从头循环

Python013

c语言程序如何让其从头循环,第1张

可以用死循环实现, 这是很常用的方法!

代码框架如下:

#include <stdio.h>

#include <stdlib.h>

int main()

{

char ch[20] = {0}

int choice = 0

while (1)

{

//...

//Add you coding here

//...

printf("1, continue 2, exit\n")

gets(ch)

choice = atoi(ch)

if (choice <= 0 || choice >=3)

{

printf("Your select error, input again!\n")

}

else if (choice == 1)

{

continue

}

else

{

exit(1)

//or

//break

}

}

return 0

}

当然情况多的话可以用case 语句实现~~~!

重复执行用循环就可以了..呵呵

例如:

#include

int

main(void)

{

char

c

c

=

getchar()

while(c!='

')//输入空格退出

{

printf("%c",

c)//这里改成你需要的那个函数做相应的工作就可以了

c

=

getchar()

}

return

0

}

用goto 配上個 lable 就好了: #include<stdio.h>main(){ double p, w, s,d,fint t REDO: printf("请输入每公里每吨货物的\n运费P,货物重w,距离s\n") scanf("%lf,%lf,%lf",&p,&w,&s) if(s<0 || w<0 || p<0) { printf("二逼呀你,怎么会有运费或货种或路程是小于0的\n") exit(0) } else if(s<250) t=0else t=s/500+1 switch(t) { case 0: d=0break case 1: d=0.02break case 2: d=0.05break case 3: d=0.08break case 4: d=0.08break case 5: d=0.1break case 6: d=0.1break default: d=0.15 } f=p*w*s*(1-d) printf("总运费f=p*w*s*(1-d)=%lf\n",f)goto REDO}