这是我创建的平均程序。
#include #include int main() { float f1,f2,f3; /* Program to calculate averages. */ /*Asks for the numbers.*/ printf(" Please enter three numbers.n"); printf ("t" "First number please.n"); scanf("%f", &f1); printf ("t" "Second number please.n"); scanf ("%f", &f2); printf("t" "Third number please.n"); scanf("%f", &f3); /* Now it averages it.*/ printf(" Thank you, wait one.n"); printf(" Excellent, your sum is.n"); printf("%f""n", f1+f2+f3); printf("Your average of the sum is now!!!!n"); printf("%f", (f1+f2+f3)/3); return 0; }
现在我会把它变成干活吗? 还是一个if else?
如果要重复整个条目和平均过程,可以围绕代码循环:
#include int main(void) { float f1,f2,f3; while (1) { printf("Please enter three numbers.n"); printf("tFirst number please.n"); if (scanf("%f", &f1) != 1) break; printf("tSecond number please.n"); if (scanf("%f", &f2) != 1) break; printf("tThird number please.n"); if (scanf("%f", &f3) != 1) break; printf("Your sum is %fn", f1+f2+f3); printf("Your average is %fn", (f1+f2+f3)/3); } return 0; }
请注意,此代码每次使用时都会检查scanf()
的返回值,如果出现问题则会中断循环。 不需要字符串连接,单个printf()
肯定可以打印字符串和值。
这是一个简单的第一阶段; 有更复杂的技术可以使用。 例如,您可以创建一个函数来提示和读取数字:
#include static int prompt_and_read(const char *prompt, float *value) { printf("%s", prompt); if (scanf("%f", value) != 1) return -1; return 0; } int main(void) { float f1,f2,f3; while (printf("Please enter three numbers.n") > 0 && prompt_and_read("tFirst number please.n", &f1) == 0 && prompt_and_read("tSecond number please.n", &f2) == 0 && prompt_and_read("tThird number please.n", &f3) == 0) { printf("Your sum is %fn", f1+f2+f3); printf("Your average is %fn", (f1+f2+f3)/3); } return 0; }
如果你想远离一组固定的三个值,那么你可以迭代直到遇到EOF或错误:
#include static int prompt_and_read(const char *prompt, float *value) { printf("%s", prompt); if (scanf("%f", value) != 1) return -1; return 0; } int main(void) { float value; float sum = 0.0; int num = 0; printf("Please enter numbers.n"); while (prompt_and_read("tNext number please.n", &value) == 0) { sum += value; num++; } if (num > 0) { printf("You entered %d numbersn", num); printf("Your sum is %fn", sum); printf("Your average is %fn", sum / num); } return 0; }
您可能还决定使用空格替换提示字符串末尾的换行符,以便在与提示符相同的行中键入该值。
如果要检查是否重复计算,可以在代码的第一个或第二个版本上使用次要变量:
#include static int prompt_and_read(const char *prompt, float *value) { printf("%s", prompt); if (scanf("%f", value) != 1) return -1; return 0; } static int prompt_continue(const char *prompt) { printf("%s", prompt); char answer[2]; if (scanf("%1s", answer) != 1) return 0; if (answer[0] == 'y' || answer[0] == 'Y') { int c; while ((c = getchar()) != EOF && c != 'n') // Gobble to newline ; return 1; } return 0; } int main(void) { float f1,f2,f3; while (printf("Please enter three numbers.n") > 0 && prompt_and_read("tFirst number please.n", &f1) == 0 && prompt_and_read("tSecond number please.n", &f2) == 0 && prompt_and_read("tThird number please.n", &f3) == 0) { printf("Your sum is %fn", f1+f2+f3); printf("Your average is %fn", (f1+f2+f3)/3); if (prompt_continue("Do you want to try again?") == 0) break; } return 0; }
你可以这样做:
int main() { float number, sum=0.0f; int index=0; do { printf ("t" "Enter number please.n"); //Asking for a number from user scanf("%f", &number); //Getting a number from a user sum+=number; //Add number entered to the sum i++; } while (i < 3); printf("Excellent, your average is %fn", sum/3); return 0; }
#include #include int main() { float f1,f2,f3; char c='Y'; /* Program to calculate averages. */ /*Asks for the numbers.*/ do { printf(" Please enter three numbers.n"); printf ("t" "First number please.n"); scanf("%f", &f1); printf ("t" "Second number please.n"); scanf ("%f", &f2); printf("t" "Third number please.n"); scanf("%f", &f3); /* Now it averages it.*/ printf(" Thank you, wait one.n"); printf(" Excellent, your sum is.n"); printf("%f""n", f1+f2+f3); printf("Your average of the sum is now!!!!n"); printf("%f", (f1+f2+f3)/3); printf ("Do you wana continue [Y/N]...n"); scanf("%c", &c); }while(c!='N'&&c!='n'); return 0; }
以上就是c/c++开发分享如何在C中使用带有用户输入的循环函数?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/549995.html