你能把我的程序修改成“使用”吗;是/否“;c语言的语句

你能把我的程序修改成“使用”吗;是/否“;c语言的语句,c,C,在这里,我正在写一个c编程代码,我需要你们的帮助!我想修改我的程序,并希望在循环中使用它,即如果用户输入“y”&如果输入“n”,则必须结束程序,我正在使用开关盒尝试此操作,我尽了最大努力结束程序,它正在返回结束,但我不知道如何使用“y”值再次运行程序 #include<conio.h> #include<stdio.h> int main() { char n, A, B; float kmTOm = 0.621371; float inchesTOf

在这里,我正在写一个c编程代码,我需要你们的帮助!我想修改我的程序,并希望在循环中使用它,即如果用户输入“y”&如果输入“n”,则必须结束程序,我正在使用开关盒尝试此操作,我尽了最大努力结束程序,它正在返回结束,但我不知道如何使用“y”值再次运行程序

#include<conio.h>
#include<stdio.h>

int main()
{
   char n, A, B;
   float kmTOm = 0.621371;
   float inchesTOfoot = 0.0833333;
   float cmTOinches = 0.393701;
   float poundtokg = 0.453592;
   float inchesTOmeter = 0.0254;
   float a, b;
   while (1)
   {
      printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. pound to kgs\n5. inches to meters\n");

      scanf(" %c", &n);
      switch (n)
      {
      case 'q':
         printf("Quitting the program...");
         goto end;
         break;
      case '1':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * kmTOm;
         printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
         break;
      case '2':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * inchesTOfoot;
         printf("%f Inches is equal to %f Foot\n", a, b);
         break;
      case '3':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * cmTOinches;
         printf("%f Cms is equal to %f Inches\n", a, b);
         break;
      case '4':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * poundtokg;
         printf("%f Pounds is equal to Kgs %f \n", a, b);
         break;
      case '5':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * inchesTOmeter;
         printf("%f inches is equal to %f meters \n", a, b);
         break;
      default:
         printf("In default now");
         break;
      }
   }
end:
   return 0;
}
#包括
#包括
int main()
{
字符n,A,B;
浮动kmTOm=0.621371;
浮动英寸英尺=0.0833333;
浮动cmTOinches=0.393701;
浮球磅塔克=0.453592;
浮子英寸流量计=0.0254;
浮子a、b;
而(1)
{
printf(“输入输入字符.q退出\n1.公里到英里\n2.英寸到英尺\n3.厘米到英寸\n4.磅到千克\n5.英寸到米\n”);
scanf(“%c”和“&n”);
开关(n)
{
案例‘q’:
printf(“退出程序…”);
转到终点;
打破
案例“1”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*kmTOm;
printf(“%.2f公里等于%.2f英里\n\n\n”,a,b);
打破
案例“2”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*英寸/英尺;
printf(“%f英寸等于%f英尺\n”,a,b);
打破
案例“3”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*cmt英寸;
printf(“%f厘米等于%f英寸\n”,a,b);
打破
案例“4”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*磅重塔克;
printf(“%f磅等于Kgs%f\n”,a,b);
打破
案例“5”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*英寸流量计;
printf(“%f英寸等于%f米\n”,a,b);
打破
违约:
printf(“现在违约”);
打破
}
}
完:
返回0;
}
我想从你们开发者那里得到一点修改来帮助我,因为我是一个初学者,从一个月开始学习,除了实际输出必须是这样----o/p-你想退出吗?按“q”,你想再次运行吗?按“y”

如果你把while(1)改成一个变量,那么你可以在每次循环后测试是否该退出

我对您的代码所做的更改都有以下注释:

#include<conio.h>
#include<stdio.h>

int main()
{
   char n, A, B;
   float kmTOm = 0.621371;
   float inchesTOfoot = 0.0833333;
   float cmTOinches = 0.393701;
   float poundtokg = 0.453592;
   float inchesTOmeter = 0.0254;
   float a, b;
   n = 'y';  //set n to make sure while repeats 
   while (n != 'n') //change while to test "n" instead of 1, this way the value of n can be changed and the loop can be exited
   {
      printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. po
und to kgs\n5. inches to meters\n");

      scanf(" %c", &n);
      switch (n)
      {
      case 'q':
         printf("Quitting the program...");
         return(0); //exit entire program
         break;
      case '1':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * kmTOm;
         printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
         break;
      case '2':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * inchesTOfoot;
         printf("%f Inches is equal to %f Foot\n", a, b);
         break;
      case '3':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * cmTOinches;
         printf("%f Cms is equal to %f Inches\n", a, b);
         break;
      case '4':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * poundtokg;
         printf("%f Pounds is equal to Kgs %f \n", a, b);
         break;
      case '5':
         printf("Enter quantity in terms of a unit\n");
         scanf("%f", &a);
         b = a * inchesTOmeter;
         printf("%f inches is equal to %f meters \n", a, b);
         break;
      default:
         printf("In default now");
         break;
      } 

        //Ask the user if they want to run again.  The code will exit if the user types 'n', it will repeat if they type anything else (including 'y').
        printf("\nRun again? y/n:");
        scanf(" %c",&n);

   } 

   return 0;
}
#包括
#包括
int main()
{
字符n,A,B;
浮动kmTOm=0.621371;
浮动英寸英尺=0.0833333;
浮动cmTOinches=0.393701;
浮球磅塔克=0.453592;
浮子英寸流量计=0.0254;
浮子a、b;
n='y';//设置n以确保在重复时
while(n!='n')//更改while以测试“n”而不是1,这样可以更改n的值并退出循环
{
printf(“输入字符.q退出\n1.kms到英里\n2.inches到foot\n3.cms到inches\n4.po
und至千克\n5英寸至米“;
scanf(“%c”和“&n”);
开关(n)
{
案例‘q’:
printf(“退出程序…”);
返回(0);//退出整个程序
打破
案例“1”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*kmTOm;
printf(“%.2f公里等于%.2f英里\n\n\n”,a,b);
打破
案例“2”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*英寸/英尺;
printf(“%f英寸等于%f英尺\n”,a,b);
打破
案例“3”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*cmt英寸;
printf(“%f厘米等于%f英寸\n”,a,b);
打破
案例“4”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*磅重塔克;
printf(“%f磅等于Kgs%f\n”,a,b);
打破
案例“5”:
printf(“输入单位数量”);
scanf(“%f”、&a);
b=a*英寸流量计;
printf(“%f英寸等于%f米\n”,a,b);
打破
违约:
printf(“现在违约”);
打破
} 
//询问用户是否希望再次运行。如果用户键入“n”,代码将退出,如果用户键入任何其他内容(包括“y”),代码将重复。
printf(“\n再次运行?是/否:”);
scanf(“%c”和“&n”);
} 
返回0;
}

大家好,欢迎来到StackOverflow。也许你可以花一些时间来使用MalkDay-Debug来编写代码——请参阅本指南,也可以考虑将所有的上限更改为普通文本……这与你的问题没有直接关系,但是在代码中使用GOTO语句通常是个坏主意。最好在while语句中检查一个布尔值,并在“q”的情况下设置该值。如果用户按
y
,您仍然需要输入一个菜单选项。事实上,您需要另一个循环来输入密钥,因为显然只有
y
n
才是可接受的响应。保持简单,并使用
default
报告“无效响应”。将
end
设置为值
false
退出程序似乎违反直觉。@我认为变量的更好名称应该是“continue”。谢谢你的建议。我会编辑它。
contonue
是一个关键字,对于变量来说也不是一个好的选择。也许只需要废弃变量,并在循环中测试
n=='y'
condition@HAL9000更优雅的解决方案,谢谢。编辑以反映。