繁体   English   中英

我怎样才能使程序请求用户输入以关闭它或从头开始启动它

[英]how can i make the program request user input to either close it or start it from the beginning

我构建了一个程序,可以对用户在关闭前输入的数字进行基本计算。 homewever,我想让程序询问用户是否要输入另一个数字,如果他说不,则关闭。 这是供参考的程序。 它要求输入四个数字,然后求和:

#include<stdlib.h>
int main(void)
{
  
  float n1, n2, n3,n4, resultado;
  
  
  printf("insira o primeiro numero: ");
  scanf("%f",&n1);
  
  printf("insira o segundo numero: ");
  scanf("%f",&n2);
  
  printf("insira o terceiro numero: ");
  scanf("%f",&n3);
  
  printf("insira o quarto numero: ");
  scanf("%f",&n4);
  
  resultado = (n1 + n2 + n3 + n4) ;
  
  
  printf(" A total soma dos numeros eh = %.1f\n",resultado);
  
  
  return 0;
}```

您可以简单地用do while循环围绕您的代码,尝试这样的事情:

int main() {
    int user_response;
    do {
        // rest of your code here

        printf("Would you like to do another calculation? (1 - yes, 0 - no) ");
        scanf("%d", &user_response);
    } while (user_response == 1);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM