利用do..while…进行多次循环
#include <stdio.h>
#include <math.h>
int main (void)
{
double a, b, c;
double delta;
double x1, x2;
char ch;
do
{
printf("请输入一元二次方程的三个系数:\n");
//printf("a = \n").输入提示不需要\n
printf("a = ");
scanf("%lf",&a);
printf("b = ");
scanf("%lf",&b);
printf("c = ");
scanf("%lf",&c);
delta = b*b - 4*a*c;
if (delta > 0)
{
x1 = (-b+ sqrt(delta)) / (2*a);
x2 = (-b- sqrt(delta)) / (2*a);
printf("有两个解,x1 = %lf, x2 = %lf",x1,x2);
}
else if (0 == delta)
{
//x1 = x2 = -b /2a;算式要加括号
x1 = x2 = (-b)/(2*a);
printf ("有一个解,x1 = x2 =%lf",x1);
}
else
{
printf("无解!\n");
}
printf("您想继续吗(Y/N):");
scanf(" %c",&ch);
}
while('y'== ch || 'Y'== ch);
return 0;
}
- 利用字符ch对循环进行判断
- 要把‘等于’与‘赋值’区分,因此有0==delta, y==ch,要把变量放在右边
- 输入字符时要有空白字符1,即scanf(” %c”,&ch);
- do…while
do
{
A
}
while(B);2
A一定会执行,然后判断B,B为真则再次执行A