我制作了一个简单的二进制到十进制转换器,过去工作正常,我使用字符串变量来声明值,但现在意识到我需要将它们存储为整数,因为我想添加到 while 循环结构中验证用户的选择,以便他们只能输入 0 或 1。
但我现在有一个程序显示无法从'int'转换为'System.IFormatProvider'
..因为我是C#的初学者,
我不知道这意味着什么,也不知道如何解决这个问题,感谢任何帮助。如果有人想看的话,这是我的代码:
int iBinaryNum; //To store binary number
int iDecimalNum; //To store decimal numbers
//Validation of user choice & main program
while (iBinaryNum == 0 || iBinaryNum == 1)
{
Console.WriteLine("Enter the binary number you want to convert to decimal");
iBinaryNum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The Binary number you have entered is " + iBinaryNum);
iDecimalNum = Convert.ToInt32(iBinaryNum, 2);
Console.WriteLine("This converted into decimal is " + iDecimalNum);
}
//If it's not equal to 0 or 1
Console.WriteLine("Invalid binary number, Please re-enter");
//Prevent program from closing
Console.WriteLine("Press any key to close");
Console.ReadKey();
最佳答案
iDecimalNum = Convert.ToInt32(iBinaryNum, 2);
您传递的参数是int, int
。如你所见here ,您可以在 Object, IFormatProvider
、String, IFormatProvider
或 String, Int32
之间进行选择。
由于第一个 int 只能用作 Object
,这意味着第二个参数必须是 IFormatProvider
。
如果您想要解决方案,则必须阐明您要尝试做什么。为什么要将整数转换为整数?
关于c# - 简单的二进制到十进制转换器 - C# 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033798/