如何声明一个每次动作完成时都会增加的变量?
How do I declare a variable that increases everytime an action is finished?
我是一个初学者,刚开始学习 c#。我想制作一个简单的控制台应用程序,将数字从十进制转换为二进制。
我已经这样做了:
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : ");
long nr_dec = long.Parse(Console.ReadLine());
int p = 0;
long[] nrbin = new long[p];
int i = 0;
while (nr_dec > 1)
{
nrbin[i] = nr_dec % 2;
i++;
nr_dec /= 2;
p++;
}
nrbin[i] = 1;
for (i = 0; i < nrbin.Length; i++)
{
Console.Write(nrbin[i]);
}
Console.ReadKey();
}
这是半途而废的。我不知道我需要在数组中存储多少个位置(例如二进制中的 5 是 101,我想存储 3 个位置),这样每次我从 % 得到一个数字时我都想让 P 增加然后数组应该有 p 个元素,但我不知道该怎么做(因为我在数组声明后增加 p 并且开头的 p 是 0 所以它存储 0 个位置)而且我也不知道如何使数组显示从最后一个到第一个的元素。
//我尝试了一个列表,但现在我得到了
索引超出范围。必须为非负数且小于集合的大小。参数名称:index
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : " );
long nr_dec = long.Parse(Console.ReadLine());
List<long> nrbin=new List<long>() ;
int i=0;
while(nr_dec> 1)
{
nrbin[i] = nr_dec % 2;
i++;
nr_dec /= 2;
}
nrbin[i] = 1;
for ( i =0;i<nrbin.Count;i++)
{
Console.Write(nrbin[i]);
}
Console.ReadKey();
}
}
}
试试这个代码
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : ");
long nr_dec = long.Parse(Console.ReadLine());
var nrbin = new List<long>();
while (nr_dec > 1)
{
var bin = nr_dec % 2;
nrbin.Add(bin);
nr_dec /= 2;
}
nrbin.Add(1);
nrbin.Reverse();
foreach (var num in nrbin)
{
Console.Write(num);
}
Console.ReadKey();
}
我没有更改您代码的逻辑,只是做了一些更改,以便您现有的算法可以正常工作。我没有使用数组,而是使用了列表并在最后反转它以便显示正确的二进制文件。
我是一个初学者,刚开始学习 c#。我想制作一个简单的控制台应用程序,将数字从十进制转换为二进制。
我已经这样做了:
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : ");
long nr_dec = long.Parse(Console.ReadLine());
int p = 0;
long[] nrbin = new long[p];
int i = 0;
while (nr_dec > 1)
{
nrbin[i] = nr_dec % 2;
i++;
nr_dec /= 2;
p++;
}
nrbin[i] = 1;
for (i = 0; i < nrbin.Length; i++)
{
Console.Write(nrbin[i]);
}
Console.ReadKey();
}
这是半途而废的。我不知道我需要在数组中存储多少个位置(例如二进制中的 5 是 101,我想存储 3 个位置),这样每次我从 % 得到一个数字时我都想让 P 增加然后数组应该有 p 个元素,但我不知道该怎么做(因为我在数组声明后增加 p 并且开头的 p 是 0 所以它存储 0 个位置)而且我也不知道如何使数组显示从最后一个到第一个的元素。 //我尝试了一个列表,但现在我得到了 索引超出范围。必须为非负数且小于集合的大小。参数名称:index
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : " );
long nr_dec = long.Parse(Console.ReadLine());
List<long> nrbin=new List<long>() ;
int i=0;
while(nr_dec> 1)
{
nrbin[i] = nr_dec % 2;
i++;
nr_dec /= 2;
}
nrbin[i] = 1;
for ( i =0;i<nrbin.Count;i++)
{
Console.Write(nrbin[i]);
}
Console.ReadKey();
}
}
}
试试这个代码
static void Main(string[] args)
{
Console.Write("Which decimal number do you want to convert into binary : ");
long nr_dec = long.Parse(Console.ReadLine());
var nrbin = new List<long>();
while (nr_dec > 1)
{
var bin = nr_dec % 2;
nrbin.Add(bin);
nr_dec /= 2;
}
nrbin.Add(1);
nrbin.Reverse();
foreach (var num in nrbin)
{
Console.Write(num);
}
Console.ReadKey();
}
我没有更改您代码的逻辑,只是做了一些更改,以便您现有的算法可以正常工作。我没有使用数组,而是使用了列表并在最后反转它以便显示正确的二进制文件。