简体   繁体   English

使用用户输入将十进制转换为二进制

[英]Converting decimal to binary with user input

I wanna make ac# application that convert a decimal number into binary with the input from the user. 我想制作一个ac#应用程序,用来自用户的输入将十进制数转换成二进制数。 I get a red squiggly line on bin, when declaring bin = Convert.ToString(decToBin,2);. 当声明bin = Convert.ToString(decToBin,2);时,我在bin上得到一条红色波浪线。 I don't understand my problem, so any help would be appreciated. 我不明白我的问题,所以任何帮助将不胜感激。

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();

i guess you haven't declared bin. 我猜你还没有宣布bin。 it should be 它应该是

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); 

This works well and waits for key after print. 这很好用,等待打印后的关键。 you were also missing a comma and some large letters. 你也错过了一个逗号和一些大字母。

   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();

in addition to what Ehsan mentioned, 除了Ehsan所说的,

Console.Readline() should be Console.ReadLine(); Console.Readline()应该是Console.ReadLine(); L caps L帽

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

This is not how you declare variables in C#. 这不是您在C#中声明变量的方式。 The syntax is 语法是

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

eg 例如

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

or 要么

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

Also see the C# language specification, §8.5.1: 另请参阅C#语言规范,§8.5.1:

The local-variable-type of a local-variable-declaration either directly specifies the type of the variables introduced by the declaration, or indicates with the identifier var that the type should be inferred based on an initializer. 局部变量声明的局部变量类型直接指定声明引入的变量的类型,或者用标识符var指示应该基于初始化器推断类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM