【发布时间】:2020-03-28 22:11:13
【问题描述】:
在这个例子中,我试图编写一个程序,让用户解决二次方程。最后,如果他们按 y/Y,他们可以重新启动程序。如果他们按 n/N,程序将退出,如果他们按任何其他,程序应该再次提示他们输入 y/Y/N/n。
不幸的是,我最终似乎无法正确运行此逻辑。任何想法为什么?谢谢
#include <ctype.h> //in order to use toupper
#include <stdio.h> // * Solution of a*x*x + b*x + c = 0 *
#include <math.h>
int main(void)
{
double a, b, c, root1, root2;
char do_again;
while (do_again == 'Y');
printf("Input the coefficient a => ");
scanf("%lf", &a);
printf("Input the coefficient b => ");
scanf("%lf", &b);
printf("Input the coefficient c => ");
scanf("%lf", &c);
if (a == 0)
{
printf("You have entered a = 0.\n");
printf("Only one root: %8.3f", -c/b);
}
else
{
root1 = (- b + sqrt(b*b-4*a*c))/(2*a);
root2 = (- b - sqrt(b*b-4*a*c))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
}
printf("Solve again (y/n)? ");
fflush(stdin);
do_again = toupper(getchar());
if (do_again !='Y' && do_again !='N' )
printf("Please try again");
do_again = toupper(getchar());
}
【问题讨论】:
-
请正确缩进你的代码!那是不可读的。
标签: c while-loop