C语言 while语句

来源:百度知道 编辑:UC知道 时间:2024/05/11 18:04:25
比如在while 语句中有a,b两个变量,我想当输入a的值为-1时跳出循环,可我编出来的代码,总是输入a=-1以后 还要我输入b的值 我该如何输入
main()
{
float gallons,miles,average;

while(gallons!=-1){
printf("Enter the gallons used(-1 to end):");
scanf("%f",&gallons);
printf("Enter the miles driven:");
scanf("%f",&miles);
average=miles/gallons;
printf("The miles/gallons for this tank was %.6f\n",average);
}

}
我想在gallons=-1的时候跳出循环 可我输入gallons=-1 却还要我输入miles
请问怎样才能在输入gallons=-1后 不输入miles直接跳出循环

main()
{
float gallons,miles,average;

while(1){
printf("Enter the gallons used(-1 to end):");
scanf("%f",&gallons);
printf("Enter the miles driven:");
if(gallons == -1)
break;
scanf("%f",&miles);
average=miles/gallons;
printf("The miles/gallons for this tank was %.6f\n",average);
}

}