質問

I wanna make a c# application that convert a decimal number into binary with the input from the user. I get a red squiggly line on bin, when declaring bin = Convert.ToString(decToBin,2);. 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. 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,

Console.Readline() should be Console.ReadLine(); L caps

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

This is not how you declare variables in C#. The syntax is

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

e.g.

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

or

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

Also see the C# language specification, §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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top