如何更改此"数字"变量,使其显示二进制数而不是"系统到字符串"?



对不起spaghettiOs!

我正在尝试执行Adventofcode任务,但这一次让我有点卡住了。我是一个编程新手,但我尽最大努力完成这项任务,所以我可能是在强迫自己做每件事。你们能帮我还是指一个正确的方向?

static void Main()
{
string path = "plik.txt";
string[] diagnostics = File.ReadAllLines(path);
int diagSize = diagnostics.Length;                      
string[] number = new string[12];
//  int result = Convert.ToInt32(diagnostics[i], 2);
for (int j = 0; j < 12; j++)
{
int ones = 0;
int zeroes = 0;
for (int i = 0; i < diagSize; i++)
{
string temp = diagnostics[i];
if (temp[j] == '1')
{
ones++;
}
else zeroes++;
}
if(ones > zeroes)
{
number[j] = "1";
}
else
{
number[j] = "0";
}
Console.WriteLine(number[j]);
}
Console.WriteLine(number);            
}             

正如您收到的第一条注释所建议的那样,您应该首先从以前构建的数组中生成一个字符串,如下所示:

string result = String.Join("", number);

现在,您可以轻松地打印该字符串,或者如果需要将其作为数字进行进一步处理,只需将其转换为十进制即可。

最新更新