在Visual Studio中重新打开项目后,控制台窗口不会出现

时间:2016-06-29 10:33:19

标签: c# visual-studio-2015

我在C#编写了一个程序,用于将二进制字符串转换为其他数字编号系统和int,它工作得非常好,然后保存并关闭它。当我重新打开它时,我得到了这个例外:

$exception {"Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read."} System.InvalidOperationException

这是因为我在程序结束时有Console.ReadKey();。但是当我删除它时,控制台窗口根本不会出现,即使程序要求用户输入,过程也会结束。以下是供参考的程序:

> using System; using System.Collections.Generic; using System.Linq;
> using System.Text; using System.Threading.Tasks;
> 
> namespace test {
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             int arr_length = 16;
>             string c_array = "\0";
>             int[] array = new int[arr_length];
>             Console.WriteLine("Input 2 byte string:");
>             for (int i = 0; i < arr_length; i++)
>             {
>                 array[i] += Convert.ToInt32(Console.ReadLine());
>                 c_array += array[i];
>             }
>             int sum = 0;
>             for (int i = 0; i < arr_length; i++)
>             {
>                 sum += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, i)); //conversion to int    
>             }
>             Console.WriteLine("The decimal representation is: " + sum);
>             Console.WriteLine("The hex representation is:" + sum.ToString("X"));
>             bool valid_BCD = true;
>             int bcd_0 = 0, bcd_1 = 0, bcd_2 = 0, bcd_3 = 0;
>             for (int i = 0; i < 4; i++)
>             {
>                 bcd_0 += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, i));
>                 if (bcd_0 > 9) valid_BCD = false;
>             }
>             for (int i = 4; i < 8; i++)
>             {
>                 bcd_1 += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, (i - 4)));
>                 if (bcd_1 > 9) valid_BCD = false;
>             }
>             for (int i = 8; i < 12; i++)
>             {
>                 bcd_2 += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, (i - 8)));
>                 if (bcd_2 > 9) valid_BCD = false;
>             }
>             for (int i = 12; i < 16; i++)
>             {
>                 bcd_3 += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, (i - 12)));
>                 if (bcd_3 > 9) valid_BCD = false;
>             }
>             if (valid_BCD)
>             {
>                 Console.WriteLine("The BCD representation is: " + "{0}{1}{2}{3}", bcd_3, bcd_2, bcd_1, bcd_0);
>             }
>             else Console.WriteLine("Error!! Cannot convert to BCD");
>             bool valid_ascii = true;
>             char ascii_0 = 'a', ascii_1 = 'a';
>             int sum_ascii = 0;
>             for (int i = 0; i < 8; i++)
>             {
>                 sum_ascii += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, i));
>                 if (sum_ascii < 127) ascii_0 = (char)sum_ascii;
>                 else valid_ascii = false;
>             }
>             sum_ascii = 0;
>             for (int i = 8; i < 16; i++)
>             {
>                 sum_ascii += array[arr_length - 1 - i] * Convert.ToInt32(Math.Pow(2, (i - 8)));
>                 if (sum_ascii < 127) ascii_1 = (char)sum_ascii;
>                 else valid_ascii = false;
>             }
>             if (valid_ascii)
>             {
>                 Console.WriteLine("The ASCII representation is: " + "{0}{1}", ascii_1, ascii_0);
>             }
>             else Console.WriteLine("Error!! Cannot convert to ASCII");
>             Console.ReadKey();
>         }
>     } }

0 个答案:

没有答案