我需要添加什么才能将输出显示给用户?我不确定需要添加什么 printf 语句才能使其工作。我知道如何向用户显示文本,但不知道如何显示计算结果。
#include <stdio.h>
#define NORMAL_ROD 10
#define SENTINEL 0
int main(void)
{
//Variable declarations
int normal_count = 0;
int normal_total = 0;
int long_count = 0;
int long_total = 0;
int short_count = 0;
int short_total = 0 ;
double normal_average;
double long_average;
double short_average;
double overall_average;
int overall_count =0;
int overall_total= 0;
int rodLength = 1;
printf("Rod Manufacturing Quality Control\n");
while(1)
{
printf("Input Rod Length: ");
scanf("%i", &rodLength);
if(rodLength == SENTINEL) break;
overall_count = overall_count + 1;
overall_total = overall_total + rodLength;
if(rodLength == NORMAL_ROD)
{
normal_count = normal_count + 1;
normal_total = normal_total + rodLength;
}
else if(rodLength > NORMAL_ROD)
{
long_count = long_count +1;
long_total = long_total + rodLength;
}
else
{
short_count = short_count +1;
short_total = short_total = rodLength;
}
}
// Average calcs
normal_average = (double)normal_total / normal_count;
short_average = (double)short_total / short_count;
long_average = (double)long_total / long_count;
overall_average = (double)overall_total / overall_count;
/*
...
...
...
*/
// Print output
printf("Statistics:\n");
printf("Desc Number Total Average\n");
return 0;
}
/*
Output here!
*/
最佳答案
要打印结果,您可以使用“格式说明符”,如 scanf()
中。
当您使用时
scanf("%d", &age);
这里%d
是整数的格式说明符。您也可以类似地对 printf()
使用格式说明符。
例如:
如果您将年龄存储在整数变量age
中,那么您可以像这样打印它:
printf("Age is: %d",age);
这里 printf()
将 ""
(第一个参数)内的内容打印到监视器。执行此操作时,%d
会被 age
的值替换。
因此,如果age
等于25
,那么将显示如下:
Age is 25
%d
是 INTERGER 变量的格式说明符。每种数据类型都有自己的格式说明符。
还有一个前任:
printf("Age is %d , weight is %d", age, weight);
所以基本上您需要在 printf()
语句中使用格式说明符来打印存储在变量中的值。如果您将结果存储在变量中,则可以使用带有正确格式说明符的这些变量名称。
您可以通过谷歌搜索“C 中的格式说明符”来查找 C 中使用的所有数据类型的格式说明符。
关于c - 我需要添加什么才能将输出显示给用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590528/