运算符(*)不能应用于类型为'对象'和'双'的操作数 [英] Operator (*) Cannot be applied to operands of type 'object' and 'double'

查看:191
本文介绍了运算符(*)不能应用于类型为'对象'和'双'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。我不明白是什么问题,但Visual Studio 2008中说,




运算符(*)不能应用于类型的操作数'对象和
'双师型




谢谢...

 #地区使用指令使用系统

;
使用System.Collections.Generic;
使用System.Text;

#endregion

命名空间DailyRate
{
类节目
{
静态无效的主要(字串[] args)
{
(新计划())的run()。
}

公共无效的run()
{
双dailyRate =的readDouble(请输入您的每日房价:);
INT noOfDays =的readInt(输入的天数:);
writeFree(CalculateFee(dailyRate,noOfDays));
}

私人无效writeFree(对象P)
{
Console.WriteLine(顾问的费用为:{0},P * 1.1);
}

私人双CalculateFee(双dailyRate,诠释noOfDays)
{
返回dailyRate * noOfDays;
}

私人诠释的readInt(串P)
{
Console.Write(P);
串线=到Console.ReadLine();
返回int.Parse(线);
}

私人双重的readDouble(串P)
{
Console.Write(P);
串线=到Console.ReadLine();
返回double.Parse(线);
}
}
}


解决方案

十六个答案为止和他们十五告诉你做错事



首先,因为每个人都告诉你了,不能由双乘的对象。 但是,解决方案不是由一个双乘双您正在做的财务的计算!; 从来没有使用财务计算双打。双打是的物理的问题,而不是的的问题。



做的正确的事情就是停止使用双打首位。您应该使用小数,而不是双击



(而当你解决这个问题:在C#标准是CapitalizeYourMethodsLikeThis,notLikeThis)


This is my code. I can not understand what is the problem, but Visual Studio 2008 says that

Operator (*) Cannot be applied to operands of type 'object' and 'double'

Thanks ...

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

解决方案

Sixteen answers so far and fifteen of them tell you to do something wrong.

First off, as everyone has told you, you cannot multiply an object by a double. But the solution is not to multiply a double by a double! You are doing a financial calculation; never use doubles for financial calculations. Doubles are for physics problems, not money problems.

The right thing to do is to stop using doubles in the first place. You should be using decimal, not double.

(And while you are fixing this problem: the standard in C# is to CapitalizeYourMethodsLikeThis, notLikeThis.)

这篇关于运算符(*)不能应用于类型为'对象'和'双'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆