C#,运算符'*';无法应用于类型为';双倍';和';十进制';
这个错误应该很简单,但我似乎不能让它工作。问题在于,同样的代码在程序的早期工作。我看不出它有任何理由在这个实例上发送错误,而不是之前的四个实例。参考下面的代码,并随时提供任何批评,你可能有,因为它应该使我更好。如果重要的话,我正在使用Sharp Develop 2.2 下面是一个有效的代码示例:C#,运算符'*';无法应用于类型为';双倍';和';十进制';,c#,decimal,multiplication,C#,Decimal,Multiplication,这个错误应该很简单,但我似乎不能让它工作。问题在于,同样的代码在程序的早期工作。我看不出它有任何理由在这个实例上发送错误,而不是之前的四个实例。参考下面的代码,并随时提供任何批评,你可能有,因为它应该使我更好。如果重要的话,我正在使用Sharp Develop 2.2 下面是一个有效的代码示例: void calc2Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(tb2_fla.Text) & Strin
void calc2Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_e.Text))
{
MessageBox.Show("Enter either kVA and Voltage or FLA and Voltage", "Invalid Data Entry", MessageBoxButtons.OK);
}
if (!String.IsNullOrEmpty(tb2_kva.Text) & !String.IsNullOrEmpty(tb2_e.Text))
{
decimal x, y, z;
x = decimal.Parse(tb2_kva.Text);
y = decimal.Parse(tb2_e.Text);
z = (x * 1000) / (1.732050808m * y); //the m at the end of the decimal allows for the multiplication of decimals
tb2_fla.Text = z.ToString();
tb2_fla.Text = Math.Round(z,2).ToString();
}
else
{
if (!String.IsNullOrEmpty(tb2_fla.Text) & !String.IsNullOrEmpty(tb2_e.Text))
{
decimal x, y, z;
x = decimal.Parse(tb2_fla.Text);
y = decimal.Parse(tb2_e.Text);
z = (x * y * 1.732050808m) / 1000; //the m at the end of the decimal allows for the multiplication of decimals
tb2_kva.Text = Math.Round(z,2).ToString();
}
下面是在本文主题行发送错误的代码示例:
void Calc4Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(tb4_fla.Text) && String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_kw.Text) & String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_e.Text))
{ //If values are entered improperly, the following message box will appear
MessageBox.Show("Enter either FLA and Voltage or kW and Voltage", "Invalid Data Entry", MessageBoxButtons.OK);
}
if (!String.IsNullOrEmpty(tb4_fla.Text)&& !String.IsNullOrEmpty(tb4_e.Text)&& String.IsNullOrEmpty(tb4_kw.Text))
{//If the user eneters FLA and Voltage calculate for kW
decimal x, y, z;
x = decimal.Parse(tb4_fla.Text);
y = decimal.Parse(tb4_e.Text);
z = (x*y)*(.8 * 1.732050808m);
tb4_kw.Text = Math.Round(z,0).ToString();
}
if (!String.IsNullOrEmpty(tb4_kw.Text) && !String.IsNullOrEmpty(tb4_e.Text) && String.IsNullOrEmpty(tb4_fla.Text))
{;//If the user enters kW and Voltage calculate for FLA
decimal x, y, z;
x = decimal.Parse(tb4_kw.Text);
y = decimal.Parse(tb4_e.Text);
z = (1000 * x)/(y * 1.732050808m)* .8;
tb4_fla.Text = Math.Round(z,0).ToString();
}
}
我很感激能得到的任何帮助。
谢谢。在这行:
.8m instead of .8
z=(xy)(0.8*1.732050808米)
将.8指定为文本,但如果没有'm'后缀,则该文本将指定双精度
z=(xy)(.8m*1.73205080m)
我会修好的。你没有说是哪一行,但我打赌这两行:
z = (x*y)*(.8 * 1.732050808m);
以及:
请注意,您的.8没有“m”限定符。我看到的其他地方都是你提供的 谢谢你指出这一点。我暗自希望事情不是那么简单m强制将文字解释为十进制。
z = (1000 * x)/(y * 1.732050808m)* .8;