C# 表达式中的编译错误

C# 表达式中的编译错误,c#,C#,因此,我遇到了一个关于体重指数计算器的小问题。我在这里搜索了其他BMI计算器主题,但没有一个对我有帮助。请记住,我对ASP.Net非常陌生,还没有真正掌握任何东西!我被告知JS会让我的生活更轻松,但我必须在ASP.net中这样做 对于BMI计算器,我使用的是英尺、英寸和磅的标准测量值。有三个文本框保存此信息。对于代码的计算部分,我希望事件处理程序检查文本框中是否只输入了数值,然后计算单个BMI。然后,计算结果应显示在第四个标题为“结果”的文本框中。下面的代码是我得到的 //***********

因此,我遇到了一个关于体重指数计算器的小问题。我在这里搜索了其他BMI计算器主题,但没有一个对我有帮助。请记住,我对ASP.Net非常陌生,还没有真正掌握任何东西!我被告知JS会让我的生活更轻松,但我必须在ASP.net中这样做

对于BMI计算器,我使用的是英尺、英寸和磅的标准测量值。有三个文本框保存此信息。对于代码的计算部分,我希望事件处理程序检查文本框中是否只输入了数值,然后计算单个BMI。然后,计算结果应显示在第四个标题为“结果”的文本框中。下面的代码是我得到的

//*************Event Handler for the calculation portion*****************

void calcUS_Click(object sender, EventArgs e)
{
    string Heightinfeet = heightus.Text;
    string Heightininches = heightus1.Text;
    string Weight = weightus.Text;

    double number;


    string bmi = resultus.Text;

    bool isHeightinfeet = Double.TryParse(Heightinfeet, out number);
    bool isHeightininches = Double.TryParse(Heightininches, out number);
    bool isWeight = Double.TryParse(Weight, out number);


    if (isHeightinfeet && isHeightininches && isWeight)
    {
        bmi = (Weight / ((Heightinfeet * 12) + Heightininches)) * ((Heightinfeet * 12) + Heightininches))) * 703);
    }

    else
    {
        Response.Write("Please type a numeric value into each of the text boxes.");
    }
}
//*****************End of calculation Event Handler*******************
除了计算机的实际计算部分,一切似乎都正常工作

if (isHeightinfeet && isHeightininches && isWeight)
{
    bmi = (Weight / ((Heightinfeet * 12) + Heightininches)) * ((Heightinfeet * 12) + Heightininches))) * 703);
}

在上面的公式中,当我将鼠标悬停在“Heightinfeet”和“Heightininches”上时,我得到的错误是“运算符”*“不能应用于'string'或'int'类型的操作数”

我能做的重构很少

int Heightinfeet;
double Heightininches;
double Weight;

if (int.TryParse(heightus.Text, out Heightinfeet) && 
    Double.TryParse(heightus1.Text, out Heightininches) && 
    Double.TryParse(weightus.Text, out Weight))
{
  bmi = (Weight / ((Heightinfeet * 12) + Heightininches)) * ((Heightinfeet * 12) + Heightininches))) * 703);
}
是的,您不能对
int
进行“*”操作,在这种情况下,数字是“12”,字符串是
Heightinfeet

因此,您应该首先将字符串解析为int或double以供使用


(int.Parse(heightinfet)*12)
或者它是Double
(Double.Parse(heightinfet)*12)

我该怎么做Simon McKenzie?我真是糊涂!谢谢你Sundeep谢谢你的帮助ebram。这似乎解决了一些问题。但是现在当我在代码中悬停在.parse上时,我得到了“double不包含解析的定义”,所以现在我有了
{bmi=(Weight/((double.parse(heightinfet)*12)+heightinches))*((heightinfet*12)+heightinches))*703;}
它说double.ParseYes有一个无效的参数不幸的是:(.我非常感谢你的帮助,{bmi=(Weight/((double.Parse(Heightinfeet)*12)+heightinches))*((Heightinfeet*12)+heightinches))*703;}At((double.Parse(高度感染)*12)我得到了一个无效的参数。为了明确起见,在数学运算中使用任何字符串之前,都应该首先将其解析为double或int。这与ASP.NET或事件处理无关。只需阅读错误消息并浏览代码,您就会发现问题。很抱歉!这是我的问题,我不理解问题所在错误因为我不熟悉这种类型的代码什么类型的代码?C#?忘记ASP.NET和事件处理,只看编译器给你一个错误的那一行。看看每个变量的类型。再次阅读错误消息。运算符
*
进行乘法运算;你已经知道了。然后错误告诉你你是C不能对
string
s和
int
s进行乘法运算。那是因为根本不能对
string
s进行乘法运算——这意味着什么?相反,你必须将
string
值转换为
int
值,然后将这两个
int
s相乘。我现在知道了,但我不知道怎么做:/