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

时间:2012-03-27 19:34:59

标签: c#

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

  

运算符(*)不能应用于'object'类型的操作数   '双'

谢谢...

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

16 个答案:

答案 0 :(得分:26)

到目前为止有16个答案,其中15个答案告诉你做错了

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

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

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

答案 1 :(得分:3)

使用

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

答案 2 :(得分:3)

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

在这种情况下,解决方案很简单,只需将writeFee参数声明为double:

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

答案 3 :(得分:3)

代码

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

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

答案 4 :(得分:3)

*没有object运算符。由于某种未知原因,您的方法需要object。在我看来它应该采取数值。所以:

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

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

...

所以选择A。

考虑一下:

writeFree("Uh oh!");

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

答案 5 :(得分:1)

你的问题在于这个方法:

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

需要修改方法的签名,以便它知道pdouble,或将p强制转换为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}", (double)p * 1.1);
}

答案 6 :(得分:0)

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

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

由于您希望将双精度传递给writeFree,因此为p声明一个合适的类型会更有意义。

private void writeFree(double p)

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

答案 7 :(得分:0)

您需要将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);
    }

答案 8 :(得分:0)

尝试

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

答案 9 :(得分:0)

像这样写:

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

pobjectdoulbe。无法将objectdouble相乘。

答案 10 :(得分:0)

变化:

    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,它不知道该怎么做。

答案 11 :(得分:0)

在线:

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

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

希望有所帮助。

答案 12 :(得分:0)

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等< / p>

答案 13 :(得分:0)

问题在于,您无法将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);
}

答案 14 :(得分:0)

您不能说您将对象乘以数值。
您所要做的就是保证参数是一个数值,因此您必须将函数参数类型更改为数字类型。
不要在函数内部使用(double)强制转换,因为如果将非数字参数传递给函数,它可能会导致奇怪的行为。干净的代码说,总是使用您需要的最具体的类型作为函数参数,以避免错误。在您的情况下,它可以是 double

答案 15 :(得分:0)

您的问题出在WriteFree方法中:

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

你不能乘以对象* 1.1

将其更改为WriteFree(双p)

或者,如果必须使用对象参数,请尝试将P解析为double。