Hello,
I was doing the money maker project for c# to use Math class and methods.
When arriving to the end and trying to print out and then doing the dotnet run
to check if everything was okay, I had an error coming from the command line.
Program.cs(15,31): error CS0121: The call is ambiguous between the following methods or
properties: 'Math.Floor(decimal)' and 'Math.Floor(double)'
[/home/ccuser/workspace/csharp-money-maker/MoneyMaker.csproj]
Just to be sure I checked both Math.Floor
I used to get the amount of coins corresponding and it seems fine.
So I just searched on and on it seems that the problem come from goldValue and silverValue type making it ambiguous for .NET? Making them as double
seems to fix it.
But the hint tells us to make these as int
. And as well as many people did, I guess we naturally did ints.
Just to be sure, here’s my original code making it bug.
using System;
namespace MoneyMaker
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to Money Maker!");
Console.WriteLine("Enter a number: ");
int amount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"{amount} cents is equal to...");
//coins values
int goldValue = 10;
int silverValue = 5;
//calculating
double goldCoins = Math.Floor(amount / goldValue);
double remainder = amount%goldValue;
double silverCoins = Math.Floor(remainder / silverValue);
remainder %= silverValue;
//output results
Console.WriteLine("Gold coins: " + goldCoins);
Console.WriteLine("Silver coins: " + silverCoins);
Console.WriteLine("Bronze coins: " + remainder);
}
}
}