【发布时间】:2019-04-09 12:03:13
【问题描述】:
我正在尝试计算 5 个数字的平均值,它们都小于 10。 任何帮助将不胜感激,因为我仍处于学习的早期阶段。
int num1, num2;
int sum, i;
float average;
printf ("Enter five numbers to calculate the average:");
for (i = 0; i < 5; i++){
scanf ("%d", num1);
if (num1 > 10){
printf ("You can't enter this number\n");
printf ("Enter five numbers to calculate the average:");
scanf ("%d", num1);
}
else{
num1=num2;
}
sum += num2;
}
average = sum / 5;
printf ("Average is: %f", average);
【问题讨论】:
-
scanf ("%d", num1);=>scanf ("%d", &num1); -
你的问题是如果用户输入大于10个数字怎么办?
-
您显示的代码有什么问题?你的问题是什么?将您的问题写在 实际的问题正文中,并保留标题对您的问题的简短摘要。请花一些时间阅读有关how to ask good questions 和this question checklist 的信息。也请学习如何创建Minimal, Complete, and Verifiable Example。
-
作为新的 c 开始检查
scanf的返回值。您的代码的另一个问题是您在num1分配了未初始化的num2。所以你失去了所有的输入。
标签: c for-loop if-statement