簡體   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