0

我想制作 ac# 应用程序,使用用户的输入将十进制数转换为二进制数。声明 bin = Convert.ToString(decToBin,2); 时,我在 bin 上看到一条红色波浪线。我不明白我的问题,所以任何帮助将不胜感激。

int decToBin;
Console.WrinteLine("Enter a number that will be converted to binary")
decToBin = Int32.Parse(Console.Readline());

bin = Convert.ToString(decToBin,2);

Console.ReadKey();
4

4 回答 4

1

我猜你还没有声明 bin。它应该是

int decToBin;
Console.WriteLine("Enter a number that will be converted to binary");
decToBin = Int32.Parse(Console.ReadLine());
string bin = Convert.ToString(decToBin, 2);
Console.WriteLine(bin); 
于 2014-03-27T11:54:01.750 回答
1

这很好用,打印后等待密钥。您还缺少一个逗号和一些大写字母。

   Console.WriteLine("Enter a number that will be converted to binary");
   var decToBin = Int32.Parse(Console.ReadLine());

   Console.WriteLine(Convert.ToString(decToBin,2));

   Console.ReadKey();
于 2016-08-02T15:02:23.823 回答
0

除了Ehsan提到的,

Console.Readline() 应该是 Console.ReadLine(); L 大写字母

于 2014-03-27T11:59:21.723 回答
0

当声明 bin = Convert.ToString(decToBin,2);。

这不是您在 C# 中声明变量的方式。语法是

<Type> <VariableName> [ = <Value>];

例如

string bin = Convert.ToString(decToBin, 2);

或者

var bin = Convert.ToString(decToBin, 2);

另请参阅 C# 语言规范,§8.5.1:

local-variable-declaration 的 local-variable-type 要么直接指定由声明引入的变量的类型,要么用标识符 var 指示应根据初始化程序推断类型。

于 2014-03-27T11:59:46.960 回答