从输出中排除用户的输入值(Excluding User's Input Value from Output)

编写交流程序,目标是多次获取用户输入,当用户输入特定年龄时,循环停止并输出用户刚输入的所有值的平均值,最小值和最大值; 但是,在确定年龄的最大/最小/平均时,程序必须忽略停止while循环的年龄。 我只能使用一个循环。 我唯一的问题是输出。 它并没有忽略具体的年龄,而是给了我不可思议的数字。 这是我编码的一部分:

#include <stdio.h> int age, avAge, minAge, maxAge, sum; sum = 0; //here is where I get the user input while (age != -1){ printf("Please enter age: \n"); scanf("%d", &age); //here is where I try to calculate the average sum += age; avAge = sum / age; //here is where I placed restrictions on the ages the user can input if (age >= 0 && age <= 100) printf("Nice try!\n"); //here is where I try to get the largest/smallest in order if (age < minAge) minAge = age; if (age > maxAge) maxAge = age; //here is where I output if the user inputs a 0 else ( age == -1){ printf("Smallest: %d\n", minAge); printf("Largest: %d\n", maxAge); printf("Average: %d\n", avAge) return 0; }

请原谅我的编码格式,因为我在我的电脑上运行了一个未安装的ubuntu,所以我在手机上。 我只是想知道我应该使用什么来阻止程序使用0作为minAge。 由于我只能使用一个循环,我认为我的选择有限。 请记住我是非常新的,所以我为我的任何无知道歉。 谢谢。

Writing a c program where the objective is to get the user's input a number of times, and when the user has input a specific age, the loop stops and outputs the average, smallest and largest of all the values the user just input; however, when determining the largest/smallest/average of the ages, the program must ignore the age that stops the while loop. I am only able to use a single loop. The only issue I am having is with the output. It is not ignoring the specific age and giving me wonky numbers. Here is a piece of my coding:

#include <stdio.h> int age, avAge, minAge, maxAge, sum; sum = 0; //here is where I get the user input while (age != -1){ printf("Please enter age: \n"); scanf("%d", &age); //here is where I try to calculate the average sum += age; avAge = sum / age; //here is where I placed restrictions on the ages the user can input if (age >= 0 && age <= 100) printf("Nice try!\n"); //here is where I try to get the largest/smallest in order if (age < minAge) minAge = age; if (age > maxAge) maxAge = age; //here is where I output if the user inputs a 0 else ( age == -1){ printf("Smallest: %d\n", minAge); printf("Largest: %d\n", maxAge); printf("Average: %d\n", avAge) return 0; }

Please forgive my coding format, I'm on my phone since I'm running an uninstalled ubuntu on my computer. I just want an idea of what I should use to keep the program from using the 0 as the minAge. Since I can only use one loop, my options are limited I think. Please keep in mind I'm very new at c so I apologize for any ignorance on my part. Thank you.

最满意答案

我认为无限循环在这里有一定程度的意义,我已经将你的打印结果语句移出循环,所以它们仍然只会在用户输入年龄后才执行。 我还纠正了if (age >= 0 && age <= 100) if (age < -1 && age > 100)并在使用之前正确初始化所有变量以及一些添加的注释,如果您还有任何其他问题一条评论 :)

#include <stdio.h> #include <limits.h> // for INT_MAX int main(void) { int age, avAge = 0, minAge = INT_MAX, maxAge = 0, sum = 0, avgCounter = 1; // avgCounter is the amount of times the loop has ran while( 1 ) //infinite loop broken if age == -1 { printf("Please enter age: \n"); scanf("%d", &age); if( age == -1 ) break; // exit loop else if (age < -1 && age > 100) printf("Nice try!\n"); // could add continue here in order to not skew the min/max and average age variables. else if (age < minAge) minAge = age; else if (age > maxAge) maxAge = age; sum += age; avAge = sum / avgCounter; avgCounter++; } printf("Smallest: %d\n", minAge == INT_MAX? 0 : minAge); // incase the user enters -1 straight away printf("Largest: %d\n", maxAge); printf("Average: %d\n", avAge); }

注意:看到“不良数字”的实际原因可能是由于未初始化的变量导致未定义的行为 ,您必须始终确保在尝试读取/使用它们之前初始化它们。

I think an infinite loop makes some level of sense here, I've moved your print result statements out of the loop so they will still only execute once the user is done inputting age s. I've also corrected if (age >= 0 && age <= 100) to if (age < -1 && age > 100) and correctly initialised all variables before use with some added comments, if you've any further questions just drop a comment :)

#include <stdio.h> #include <limits.h> // for INT_MAX int main(void) { int age, avAge = 0, minAge = INT_MAX, maxAge = 0, sum = 0, avgCounter = 1; // avgCounter is the amount of times the loop has ran while( 1 ) //infinite loop broken if age == -1 { printf("Please enter age: \n"); scanf("%d", &age); if( age == -1 ) break; // exit loop else if (age < -1 && age > 100) printf("Nice try!\n"); // could add continue here in order to not skew the min/max and average age variables. else if (age < minAge) minAge = age; else if (age > maxAge) maxAge = age; sum += age; avAge = sum / avgCounter; avgCounter++; } printf("Smallest: %d\n", minAge == INT_MAX? 0 : minAge); // incase the user enters -1 straight away printf("Largest: %d\n", maxAge); printf("Average: %d\n", avAge); }

NOTE: The actual reason for seeing "wonky numbers" is probably due to undefined behaviour thanks to uninitialised variables, you must always make sure they are initialised before attempting to read/use them.

更多推荐