二进制转十进制编程C#
这是我写的代码 半忙看看是否有问题 谢谢了using System;
using System.Collections.Generic;
using System.Text;
namespace jingzhishuzhuanhuan
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个二进制数");
string number = Console.ReadLine();
bool flag = false;
for (int i = 0; i < number.Length; i++)
{
string n = number.Substring(i, 1);
int c = int.Parse(n);
if (c < 0 || c > 1)
{
flag = true;
break;
}
}
if (flag)
{
Console.WriteLine("输入的这个数不是二进制数,数字为" + number);
double x = 0;
for (int i = 0; i < number.Length; i++)
{
string n = number.Substring(i, 1);
double c = double.Parse(n);
x = x + c * Math.Pow(2, number.Length - i - 1);
}
Console.WriteLine("二进制数" + number + "转换为十进制数为" + x);
}
else
{
Console.WriteLine("输入的这个数是二进制数,数字为" + number);
}
}
}
}
补充: 我自己粗心