运算符(*)不能应用于’object’和’double’类型的操作数

这是我的代码。 我无法理解是什么问题,但Visual Studio 2008说明了这一点

运算符(*)不能应用于’object’和’double’类型的操作数

谢谢 …

#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); } } } 

到目前为止十六个答案, 十五个答案告诉你做错了

首先,正如大家告诉你的那样,你不能将对象乘以双倍。 但解决方案不是将双倍乘以一倍! 你正在进行财务计算; 从不使用双打进行财务计算 。 双打是物理问题,而不是金钱问题。

正确的做法是首先停止使用双打。 你应该使用decimal ,而不是double

(当你解决这个问题时:C#中的标准是对CapitalizeYourMethodsLikeThis,而不是喜欢这个。)

你的writeFee方法无法编译。 你不能将一个object与一个数字相乘,编译器不知道如何 – 一个对象可以是任何东西而可能不是一个数字。

在这种情况下,解决方案很简单,只需将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); } 

只要你用双打调用函数就可以工作。

object没有*运算符。 您的方法因某种未知原因而占用一个object 。 在我看来它应该采取数值。 所以要么:

A)更改writeFree方法以采用适当的数字类型,或B)在乘法之前将对象转换为适当的数字类型。

当然,考虑到你向我们展示的例子,选项B实际上只是一个穷人的选项A版本。

所以选择A.

考虑一下:

 writeFree("Uh oh!"); 

您对"Uh oh!" * 1.1的结果有什么期望"Uh oh!" * 1.1 "Uh oh!" * 1.1是? 如果您的代码已编译,那将是合法的。

使用

  Console.WriteLine("The consultant's fee is: {0}", (double) p * 1.1); 

你的问题在于这个方法:

 private void writeFree(object p) { Console.WriteLine("The consultant's fee is: {0}", p * 1.1); } 

需要修改方法的签名,以便它知道pdouble ,或者将pdouble

 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}", (double)p * 1.1); } 

如果要在数学运算中使用p ,则必须将p为数字类型。

 Console.WriteLine("The consultant's fee is: {0}", ((double)p) * 1.1); 

因为看起来你期望将一个double传递给writeFree ,所以在那里为p声明一个合适的类型会更有意义。

 private void writeFree(double p) 

这将消除对施法操作的需要。

你需要将p转换为double

 Console.WriteLine("The consultant's fee is: {0}", (double)p * 1.1); 

或者更改你的writeFree以接受double并且它将起作用并且这也是有意义的,因为你的CalculateFee已经返回一个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.Parse(p) * 1.1); 

像这样写:

 Console.WriteLine("The consultant's fee is: {0}", ((double)p * 1.1)); 

这条线上的p是一个object不是一个doulbe 。 不能将object乘以double

更改:

  private void writeFree(object p) { Console.WriteLine("The consultant's fee is: {0}", p * 1.1); } 

至:

  private void writeFree(double p) { Console.WriteLine("The consultant's fee is: {0}", p * 1.1); } 

正如您目前编写的那样,编译器认为您正在尝试将对象乘以1.1,它不知道该怎么做。

在线上:

 Console.WriteLine("The consultant's fee is: {0}", p * 1.1); 

您应该将p(它是一个对象)转换为double或其他数字类型。

希望有所帮助。

 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等。

问题是你不能将object乘以double 。 从代码用法来看,你应该改变

 private void writeFree(object p) { Console.WriteLine("The consultant's fee is: {0}", p * 1.1); } 

 private void writeFree(double 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。