how can read values and put them in Array C#

依然范特西╮ 提交于 2021-02-16 15:34:18

问题


I start dicoverig C# today ... as you know there is some difuculties

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
       int[] arr = new int[3];
            int i;
            for (i=0; i < 3; i++)
            {
                arr[i] = Console.Read();
            }
            for (int k = 0; k < 3; k++)
            {
                Console.Write(arr[k]);
            }     

            Console.ReadKey();
        }

    }
}

this code didn't work with me when i compiled it ...it make me put value for one time then he print other values ! any one here to help


回答1:


try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] arr = new int[3];
      int i;
      for (i = 0; i < 3; i++)
      {
        arr[i] = Convert.ToInt32(Console.ReadLine());
      }
      for (int k = 0; k < 3; k++)
      {
        Console.WriteLine(arr[k]);
      }

      Console.ReadKey();
    }

  }

}

Bye.




回答2:


If i understand you correcrtly this is what you want

int[] arr = new int[3];
            for (int i = 0; i < 3; i++)
                arr[i] = Convert.ToInt32(Console.ReadLine());
            for (int k = 0; k < 3; k++)
                Console.WriteLine(arr[k]);

            Console.ReadKey();



回答3:


Instead of

arr[i] = Console.Read();

try this:

arr[i] = (int)Console.ReadKey().KeyChar;

Console.Read() will continue to read the same character over and over as it will still be considered the next character from the input stream. The Console.ReadKey() function reads keys as they are pressed.



来源:https://stackoverflow.com/questions/1382515/how-can-read-values-and-put-them-in-array-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!