C# 运算符(*)不能应用于类型为';对象';和';双倍';
这是我的密码。我不明白问题出在哪里,但VisualStudio2008说 运算符(*)不能应用于“object”类型和 “双人” 谢谢C# 运算符(*)不能应用于类型为';对象';和';双倍';,c#,C#,这是我的密码。我不明白问题出在哪里,但VisualStudio2008说 运算符(*)不能应用于“object”类型和 “双人” 谢谢 #region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace DailyRate { class Program { static void Main(strin
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace DailyRate
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
public void run()
{
double dailyRate = readDouble("Enter your daily rate: ");
int noOfDays = readInt("Enter the number of days: ");
writeFree(CalculateFee(dailyRate, noOfDays));
}
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private double CalculateFee(double dailyRate, int noOfDays)
{
return dailyRate * noOfDays;
}
private int readInt(string p)
{
Console.Write(p);
string line = Console.ReadLine();
return int.Parse(line);
}
private double readDouble(string p)
{
Console.Write(p);
string line = Console.ReadLine();
return double.Parse(line);
}
}
}
如果要在数学运算中使用
p
,则必须将其转换为数字类型
Console.WriteLine("The consultant's fee is: {0}", ((double)p) * 1.1);
由于看起来您希望将double传递给writeFree
,因此在那里为p
声明一个合适的类型更有意义
private void writeFree(double p)
这样就不需要强制转换操作。使用
Console.WriteLine("The consultant's fee is: {0}", ((double)p) * 1.1);
Console.WriteLine("The consultant's fee is: {0}", (double) p * 1.1);
编译失败的是您的
writeFee
方法。您不能将对象
与数字相乘,编译器不知道如何-对象可以是任何东西,也可能不是数字
在这种情况下,解决方案很简单,只需将writeFee参数声明为double:
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
在代码中
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
p是一个对象,编译器不知道如何将一个对象和一个double相乘
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
只要您使用double调用函数,它就可以工作。对于
对象
没有*
操作符。由于未知原因,您的方法获取了一个对象。在我看来,它应该是一个数值。因此,要么:
A) 更改writeFree
方法以采用适当的数字类型,或
B) 在相乘之前将对象强制转换为适当的数字类型
当然,考虑到你给我们展示的例子,选项B实际上只是选项a的穷人版本
所以选择A
考虑这一点:
writeFree("Uh oh!");
你希望的结果是什么?如果您的代码经过编译,则是合法的。您需要将p转换为双精度
Console.WriteLine("The consultant's fee is: {0}", (double)p * 1.1);
或者将您的writeFree
更改为接受一个double
,这样做也会起作用,因为您的calculatefree
已经返回了一个double
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
试一试
这样写:
Console.WriteLine("The consultant's fee is: {0}", ((double)p * 1.1));
p
此行是一个对象
,而不是doulbe
。不能将对象
与double
相乘更改:
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
致:
正如您目前编写的那样,编译器认为您正在尝试将一个对象乘以1.1,但它不知道如何做。您的问题在于此方法:
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
需要修改方法的签名,以便它知道p
是double
,或者将p
转换为double
:
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
或
在线:
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
您应该将p(对象)强制转换为双精度或其他数字类型
希望有帮助
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
p是一个对象(参见方法签名)。如果你知道类型是什么,那么你可以简单地强制转换
Console.WriteLine("The consultant's fee is: {0}", (double)p * 1.1);
我在那里做了双重测试,但您可以使用float、int等。如果您不知道类型是什么,那么您需要使用'as'或'is'/typeof等进行简单的类型测试。问题是,您不能将对象
乘以双重。从代码使用情况来看,您应该更改
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
到
你不能说你把一个物体乘以一个数值
您所要做的就是保证参数是数值,因此您必须将函数参数类型更改为数值类型。
不要在函数内部使用(double)强制转换,因为如果将非数字参数传递给函数,可能会导致奇怪的行为。干净的代码表示,始终使用您需要的最特定类型作为函数参数,以避免错误。在您的情况下,它可能是双重的。您的问题在于WriteFree方法:
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
不能将对象*1.1相乘
将其更改为WriteFree(双p)
或者,如果你必须接受一个宾语参数,可以尝试将p解析为double。到目前为止有16个答案,其中15个告诉你做错了什么
首先,正如每个人都告诉过你的,你不能把一个物体乘以一个双倍体但解决方案不是将一个双精度乘以一个双精度您正在进行财务计算不要在财务计算中使用双精度。双打是物理问题,不是金钱问题
正确的做法是首先停止使用双打。您应该使用十进制
,而不是双精度
private void writeFree(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
(当你解决这个问题时:C中的标准是将你的方法大写,而不是像这样。)这是非常重要的一点信息。埃里克的答案通常不需要额外的支持,但我经常看到这一点;用于表示货币值的浮点数。糟糕@安德鲁巴伯:事实上,问题不在于使用浮点来表示货币,而在于使用二进制浮点而不是十进制浮点。毕竟,您不太可能处理的金额如此之大,以致于该点的浮动性质会产生影响。问题是二进制浮点不能精确地表示在处理货币时可能出现的值(这些值是我们的10进制直觉告诉我们是精确的)。此外,四舍五入规则不太可能满足货币价值的要求。
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}