本文介绍了运算符(*)不能应用于类型为'对象'和'双'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码。我不明白是什么问题,但Visual Studio 2008中说,
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.)
这篇关于运算符(*)不能应用于类型为'对象'和'双'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!