【发布时间】:2011-12-18 02:10:01
【问题描述】:
问题是:Totals 游戏可以由任意数量的人玩。它从总共 100 开始,然后每个玩家依次在 -20 到 20 之间进行整数位移。获胜者是调整使总数等于 5 的玩家。仅使用给定的三个变量: 全部的 调整 柜台 这是我到目前为止所拥有的:
#include <stdio.h>
int main (void)
{
int counter=0;
float adj;
int ttl=100;
printf("You all know the rules now lets begin!!!\n\n\nWe start with 100. What is\n");
while (ttl!=5)
{
printf("YOUR ADJUSTMENT?");
scanf("%f",&adj);
counter++;
if (adj<=20 && adj>=-20)
{
ttl=ttl+adj;
printf("The total is %d\n",ttl);
}
else
{
printf ("I'm sorry. Do you not know the rules?\n");
}
}
printf("The game is won in %d steps!",counter);
}
我需要什么: 当输入十进制数时,它会转到 else。如何确定浮点数是否有小数部分。
【问题讨论】:
-
在二进制级别,没有浮点数有小数。 en.wikipedia.org/wiki/IEEE_754-2008
-
我们在这里谈论浮动。它总是“有小数”。请改用
double。 (仅供参考@Chris,我的意思是一样的) -
@TomvanderWoerdt:
double有什么不同? -
@OliCharlesworth 每个人都一直告诉我,双精度和浮点数的区别在于浮点数有精度误差,而双精度数允许您存储更少的数据。如果我错了,请纠正我。
-
只是出于好奇,为什么那个程序中有
float?只涉及整数。
标签: c floating-point floating-point-conversion