任何人都可以帮忙编写以下代码吗?
来自 python 的 c 新手,所以我知道我的代码是错误的。
我试图编写一个代码,可以将 float 舍入并将字符乘以所述 float
float avg;
char star='*';
float top;
float bottom;
top=50;
bottom=80;
avg=(top/bottom*10);
star*=avg;
printf("%c\n",star);
printf("%d\n",top);
printf("%d\n",bottom);
printf("%f\n",avg);
输出需要
* * * * * *
50
80
6.25
我得到的是
♠
50
80
6.25
我还尝试使用下限来舍入平均值,但这只会将黑桃变成不同的字符,6.25 变成 6.00000000000000
最佳答案
您应该使用循环来显示您的 *
;
for( int i = 0; i < avg; ++ i ) {
printf( "* " );
}
printf( "\n" );
(去掉star
变量)。
解释一下您所看到的行为:当您执行 star *= avg;
时,实际发生的是 star 的 ascii 值,即 *
=> 42,乘以 avg,即 6.25。这会产生 262.5,它被缩短(模 256)为 6.5,并删除小数点 (6),以再次适合 char。 ascii 中的 6 通常不是可见字符(从技术上讲,它是“确认”),但在您使用的代码页中,事实证明它被绘制为黑桃符号。
关于c - 将 float 舍入并乘以它的字符(c)* 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15593945/