当试图运行这个时,我的const会变得圆润 从double转换为int。有没有办法避免 从发生中走出来?
const double ACTIVTY_LEVEL1 = 1.2;
const double ACTIVTY_LEVEL2 = 1.375;
const double ACTIVTY_LEVEL3 = 1.55;
const double ACTIVTY_LEVEL4 = 1.725;
const double ACTIVTY_LEVEL5 = 1.9;
int activityLevel = lstActivityLevel.SelectedIndex;
switch (activityLevel)
{
case 0:
activityLevel = Convert.ToInt32(ACTIVTY_LEVEL1);
break;
case 1:
activityLevel = Convert.ToInt32(ACTIVTY_LEVEL2);
break;
case 2:
activityLevel = Convert.ToInt32(ACTIVTY_LEVEL3);
break;
case 3:
activityLevel = Convert.ToInt32(ACTIVTY_LEVEL4);
break;
case 4:
activityLevel = Convert.ToInt32(ACTIVTY_LEVEL5);
break;
}
答案 0 :(得分:1)
在我看来,当你不应该重新使用activityLevel
变量时。我怀疑你真的double
是double[] activityLevels = new [] { 1.2, 1.375, 1.55, 1.725, 1.9 };
double activityLevel = activityLevels[lstActivityLevel.SelectedIndex - 1];
。
以下是您应该如何对此进行编码:
{{1}}
答案 1 :(得分:0)
整数不能有小数位,因此从double
转换为int
时,小数位后的所有内容都会被删除。
答案 2 :(得分:0)
您似乎希望将activityLevel
设置为const
varialbe之一。这是方法:
const double ACTIVTY_LEVEL1 = 1.2;
const double ACTIVTY_LEVEL2 = 1.375;
const double ACTIVTY_LEVEL3 = 1.55;
const double ACTIVTY_LEVEL4 = 1.725;
const double ACTIVTY_LEVEL5 = 1.9;
double activityLevel = 0;
switch (lstActivityLevel.SelectedIndex)
{
case 0:
activityLevel = ACTIVTY_LEVEL1;
break;
case 1:
activityLevel = ACTIVTY_LEVEL2;
break;
case 2:
activityLevel = ACTIVTY_LEVEL3;
break;
case 3:
activityLevel = ACTIVTY_LEVEL4;
break;
case 4:
activityLevel = ACTIVTY_LEVEL5;
break;
}
lblBMRResult.Text = (BMR * activityLevel).ToString();