第一种
#include <stdio.h>
#include<math.h>
int main()
{
float x, y;
printf("输入");
scanf_s("%f", &x);
while (x >= 1000) //当数字大于1000时,就会重新输入,直到输入的数小于1000.
{
printf("请重新输入");
scanf_s("%f", &x);
}
y = sqrt(x);
printf("y=%f", y);
return 0;
}
第二种
#include<stdio.h>
#include<math.h>
int main()
{
float a,b;
while(100)
{
printf("请输入一个数字:\n");
scanf_s("%f", &a);
if (a > 0&&a<=1000)
{
b = sqrt(a);
printf("根为%5.2f", b);
}
else
printf("请重新输入:");
}
return 0;
}
第三种
#include<stdio.h>
#include<math.h>
int main()
{
float a,d;
int c,b=1000;
printf("请输入一个数字:");
while (10)
{
scanf_s("%f", &a);
c = a / b;
d = sqrt(a);
switch (c)
{
case 0:printf("您的根为%5.2f", d); break;
default:printf("请重新输入:");
}
}
return 0;
}
第二种比第一种好在利用了while循环语句,可以实现多次进行输入。
第三种比第二种又加了一个switch