c# - 简单的二进制到十进制转换器 - C# 中的问题

标签 c# loops while-loop int

我制作了一个简单的二进制到十进制转换器,过去工作正常,我使用字符串变量来声明值,但现在意识到我需要将它们存储为整数,因为我想添加到 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, IFormatProviderString, IFormatProviderString, Int32 之间进行选择。

由于第一个 int 只能用作 Object,这意味着第二个参数必须是 IFormatProvider

如果您想要解决方案,则必须阐明您要尝试做什么。为什么要将整数转换为整数?

关于c# - 简单的二进制到十进制转换器 - C# 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033798/

相关文章:

C# 显示带有 WS_EX_NOACTIVATE 标志的表单

C# 如何确保用户提供线程安全对象来运行

javascript - 我的reverse()函数有什么问题吗?

c# - 从外部项目连接到现有 MVC 5 数据库(身份模型)

c# - 如何循环我的音频文件?

javascript - 如何为循环的循环添加计数器编号?

javascript - ReactJS - onClick 不调用 map() 函数内的函数

Javascript暂停循环直到事件发生

c - 有没有更简单的方法来做到这一点?我怎样才能摆脱我的 while 循环之一?

java - Java 中 "while"的问题