【问题标题】:how do I take input for an array and print it? [duplicate]如何获取数组的输入并打印它? [复制]
【发布时间】:2021-11-04 15:50:32
【问题描述】:
int i, c; string b;
Console.WriteLine("enter no of elements");
c = Console.Read();
string[] s = new string[c];
for (i = 0; i < s.Length; i++)
{
    Console.WriteLine("enter elements");
    b = Console.ReadLine();
    s[i] = b;
}
Console.WriteLine("Array : ");
Console.WriteLine("[{0}]", string.Join(", ", s));
//Console.Write(s);

我正在尝试获取数组的输入并打印相同的内容,但它会产生无限循环。

【问题讨论】:

    标签: c#


    【解决方案1】:

    正如另一个答案指出的那样,输入被视为 ASCII

    尝试更改此行

    c = Console.Read();
    

    到这里

    c = Convert.ToInt32(Console.ReadLine());
    

    【讨论】:

      【解决方案2】:

      当你这样做时,你必须小心:

      c = Console.Read();
      

      然后你输入 5 那么 c 的值不是 5 而是 53...因为你没有读取 int

      记住 5 因为 ascii 映射到整数中的 53

      【讨论】:

        猜你喜欢
        • 2021-07-01
        • 1970-01-01
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多