>编写一个c程序,其目标是多次获取用户的输入,当用户输入特定的年龄时,循环停止并输出用户刚刚输入的所有值中的平均值,最小值和最大值;但是,在确定年龄的最大/最小/平均值时,程序必须忽略停止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: %dn", minAge);
printf("Largest: %dn", maxAge);
printf("Average: %dn", avAge)
return 0;
}
请原谅我的编码格式,我在手机上,因为我在我的计算机上运行一个卸载的 ubuntu。我只是想了解我应该使用什么来防止程序使用 0 作为 minAge。由于我只能使用一个循环,因此我认为我的选择是有限的。请记住,我在c很新,所以我对我的任何无知表示歉意。谢谢。
我认为无限循环在这里有一定程度的意义,我已经将您的打印结果语句移出了循环,因此它们仍然只会在用户完成输入年龄 s 后执行。我还更正了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: %dn", minAge == INT_MAX? 0 : minAge); // incase the user enters -1 straight away
printf("Largest: %dn", maxAge);
printf("Average: %dn", avAge);
}
注意:看到"不稳定的数字"的实际原因可能是由于未初始化的变量而导致的未定义行为,在尝试读取/使用它们之前,您必须始终确保它们已初始化。
编辑 :当前代码中存在太多与逻辑和语法相关的小错误。以下代码按您的要求按预期工作。
#include <stdio.h>
int main(){
int age, avAge, minAge, maxAge, sum;
minAge = 100000 ; // some very large number
maxAge = -1; // initailize with min possible age
sum = 0;
int iteration = 0 ; // no. of times the loops run with valid input from user
do{
printf("Please enter age: , -1 to quit n");
scanf("%d", &age);
if (age >= 0 && age <= 100) {
printf("Nice try!n");
iteration ++;
sum += age;
avAge = sum / age;
if (age < minAge)
minAge = age;
if (age > maxAge)
maxAge = age;
}
else {
printf("Smallest: %dn", minAge);
printf("Largest: %dn", maxAge);
printf("Average: %dn", avAge);
break ;
}
}
while( age > 0 && age <= 100) ;
return 0;
}
关于 ideone 的工作示例。
假设有效年龄在 (0, 100]
范围内,下面是一个实现:
#include <stdio.h>
int main(void) {
int age, minAge, maxAge, sum, avAge;
sum = 0;
int i = 0; // counts the number of ages read
printf("Please enter age: ");
while (scanf("%d", &age)) {
// if age is in range (0, 100], consider valid
// and invalid otherwise
if (age > 0 && age <= 100) {
// calculate sum
sum += age;
// if this is the first loop round (i == 0), initialize
// both minAge & maxAge to 'age', since they
// don't yet have a valid value
if (i == 0) {
minAge = age;
maxAge = age;
++i;
} else {
if (age < minAge) {
minAge = age;
}
if (age > maxAge) {
maxAge = age;
}
++i;
}
} else if (age == 0) {
// if age is zero, break/exit loop
// zero can be changed to any invalid value (eg. -1)
break;
} else {
printf("Nice try!n");
}
printf("Please enter age: ");
}
// calculate average (outside loop)
// average = sum / number of ages read
avAge = sum / i;
// print summary
printf("Smallest: %dn", minAge);
printf("Largest: %dn", maxAge);
printf("Average: %dn", avAge);
return 0;
}