在C上上课,我们学习了if / else语句,重复,运算符,循环和切换。
此分配的目的是让用户输入5个数字和后面的输出:
显示输入的最小数字
显示输入的最大数字
显示输入的五个数字的总和
显示输入的五个数字的平均值
我知道它很长并且可能陷入困境但是它运行并适用于所有人,除了' minim'它反复输出零而不是实际的最小数字。不知道我在哪里出错了。任何提示都会有帮助。
int choice = 0, number, sum = 0, count = 0, minim = 0, max = 0;
float average;
printf("Please enter your first number: ");
scanf("%i", &number);
if (number < minim) {
minim = number; }
if (max < number) {
max = number; }
count += number;
printf("Please enter your second number: ");
scanf("%i", &number);
if (number < minim) {
minim = number; }
if (max < number) {
max = number; }
count += number;
printf("Please enter your third number: ");
scanf("%i", &number);
if (number < minim) {
minim = number; }
if (max < number) {
max = number; }
count += number;
printf("Please enter your fourth number: ");
scanf("%i", &number);
if (number < minim) {
minim = number; }
if (max < number) {
max = number; }
count += number;
printf("Please enter your fifth number: ");
scanf("%i", &number);
if (number < minim) {
minim = number; }
if (max < number) {
max = number; }
count += number;
sum += count;
average = sum / 5;
while (choice != -1) {
printf("\n\nChoose an option from the menu below to see the results (1,2,3,4) or use (-1) to exit:\n");
printf("1. Smallest number entered\n");
printf("2. Largest number entered\n");
printf("3. Sum of the five numbers entered\n");
printf("4. Average of the five numbers entered\n\n");
printf("\nEnter your selection: ");
scanf("%i", &choice);
switch (choice) {
case 1:
printf("\n The smallest number entered is %i\n", minim);
break;
case 2:
printf("\n The largest number entered is %i\n", max);
break;
case 3:
printf("\n The sum of the five numbers entered is %i\n", sum);
break;
case 4:
printf("\n The average of the five numbers entered is %.2lf\n", average);
break;
default:
printf("Incorrect menu option selected.\n");
}
}
printf("\nThank you for your time. Exiting program.\n");
}
答案 0 :(得分:2)
正如John3136所提到的,你可以将初始最小值分配给常量INT_MAX
,将初始最大值分配给INT_MIN
,你可能应该这样做,因为它会使转换更容易,但是使用当前代码的更简单的替代方法是简单地将用户输入的初始值分配给minim
。如果您使用max
执行此操作也会有所帮助,因为它可以让您的程序处理负数。
答案 1 :(得分:1)
minim
从0开始,只有在数字小于0时才会重置,除非您输入负数...
2种方法:
minim
设置为INT_MAX(一切都将小于INT_MAX)minim
设置为某个“不可能”的标记值(例如-1),以便您第一次知道if (minim == sentinel)
,然后设置minim
INT_MAX位于limits.h
- 有关更多详细信息,请参阅此qustion Is there a LARGEST_INTEGER macro or something similar? (C)
没有1是最简单的,适用于所有情况。
正如评论中所指出的:所写的代码是直接顺序的,所以你可以在“输入第一个数字块”中说minim = number;
。我不建议这样做的原因是,如果/当OP将数据条目放入循环时,您需要在i=0
情况下进行特殊情况处理。我想使用哨兵并没有那么不同,所以INT_MAX
是唯一明智的答案。
答案 2 :(得分:0)
只需将第一个数字设为最小数字,如下面的代码:
printf("Please enter your first number: ");
scanf("%i", &number);
**minim=number;**
答案 3 :(得分:0)
而不是在number
使用循环中输入5个输入,并将值minim
和max
设置为第一个输入。
我稍微更改了代码,所以我给出了完整的代码。
int main() {
int choice , number, sum = 0, count = 0, minim = 0, max = 0;
float average;
for(int i=0;i<5;i++){
printf("Enter %d number\n",i+1);
scanf("%d", &number);
if(i==0){
max=number;
minim=number;
}
else{
if(number>max){
max=number;
}
if(number<minim){
minim=number;
}
}
sum+=number;
}
average=sum/5;
while (1) { // infinite loop
printf("\n\nChoose an option from the menu below to see the results (1,2,3,4) or use (-1) to exit:\n");
printf("1. Smallest number entered\n");
printf("2. Largest number entered\n");
printf("3. Sum of the five numbers entered\n");
printf("4. Average of the five numbers entered\n\n");
printf("\nEnter your selection: ");
scanf("%i", &choice);
switch (choice) {
case 1:
printf("\n The smallest number entered is %i\n", minim);
break;
case 2:
printf("\n The largest number entered is %i\n", max);
break;
case 3:
printf("\n The sum of the five numbers entered is %i\n", sum);
break;
case 4:
printf("\n The average of the five numbers entered is %.2lf\n", average);
break;
case -1:
printf("\n Program Exit\n");
exit(0);
default:
printf("\nIncorrect menu option selected.\n");
}
}
printf("\nThank you for your time. Exiting program.\n");
return 0;
}
希望有所帮助。