【问题标题】:Inputing a selection from an array c#从数组c#中输入选择
【发布时间】:2014-05-21 12:29:46
【问题描述】:

您好,我是 C# 新手,如果我的问题有一个非常明显的答案,请多多包涵。

我正在尝试编写一个代码,该代码向用户显示一组选项,并带有一个数字来选择选项。然后用户选择他们的选项,代码将显示他们从数组中的选择。

例如

Select 1 for apples
Select 2 for oranges
Select 3 for pears

Please Enter your Selection :

我一直在输入一个 for 循环来显示我的区域,但我无法让我的程序从数组中读取输入,这就是我目前所拥有的

static void Main(string[] args) {

  string[] fruit = [3]

  fruit[0]=("apples");
  fruit[1]=("oranges");
  fruit[2]=("pears");

  for (int i = 0; i < fruit.length; i++)
  {
    Console.WriteLine("Enter {0} to select {1}", i, fruit[i]);
  }

  string choice = Console.ReadLine();
  int i = int.Parse(choice);
  Console.WriteLine("You have selected {0} which is {1}", i, fruit[i]);
  Console.ReadLine();
}

这给了我一个错误,如果我把它放在 for 循环中,那么程序不会显示我所有的选项。

【问题讨论】:

标签: c# arrays for-loop input output


【解决方案1】:

您需要为循环变量或用户选择指定不同的名称。

您也可能想使用TryParse 而不是Parse 来防止可能出现的FormatExceptions

int userChoice;
if(int.TryParse(choice, out userChoice) && userChoice < fruit.Length)
      Console.WriteLine("You have selected {0} which is {1}", userChoice, fruit[userChoice]);

【讨论】:

    【解决方案2】:
    void Main()
    {
        // compile a list of possible fruits (remember that c# arrays are
        // 0-indexed (first element is actually 0 and not 1))
        string[] fruits = new[]{
            "apples",
            "oranges",
            "pears"
        };
    
        // iterate over the options and output each one
        for (var i = 0; i < fruits.Length; i++){
            // add one to index value to make it non-0 based
            Console.WriteLine("Enter {0} to select {1}", i + 1, fruits[i]);
        }
    
        // continue to read user input until they select a valid choice
        Int32 choice = 0;
        do
        {
            // read user input
            String input = Console.ReadLine();
            // [try to] convert the input to an integer
            // if it fails, you'll end up with choice=0
            // which will force the while(...) condition to fail
            // and, therefore, retry for another selection.
            Int32.TryParse(input, out choice);
        }
        // number should be between 1 and (fruits.Length + 1)
        while (choice < 1 || choice > (fruits.Length + 1));
    
        // to reference it, subtract 1 from user's choice to get back to
        // 0-indexed array
        Console.WriteLine("You have selected {0} which is {1}", choice, fruits[choice - 1]);
    }
    

    【讨论】:

      【解决方案3】:

      您的字符串数组声明不正确。试试

      string[] fruit = new string[3];
      

      或者您可以这样声明并启动:

      string[] fruit = new string[3]{"apples", "oranges", "pears"};
      

      或更简单:

      string[] fruit = {"apples", "oranges", "pears"};
      

      【讨论】:

        【解决方案4】:

        您有多个拼写错误,您不能使用变量i 两次。

        试试这个:

        static public void Main(string[] args) 
        {
        
          string[] fruit = new string[3];
        
          fruit[0]=("apples");
          fruit[1]=("oranges");
          fruit[2]=("pears");
        
          for (int i = 0; i < fruit.Length; i++)
          {
            Console.WriteLine("Enter {0} to select {1}", i, fruit[i]);
          }
        
          string choice = Console.ReadLine();
          int j = int.Parse(choice);
          Console.WriteLine("You have selected {0} which is {1}", j, fruit[j]);
          Console.ReadLine();
        }
        

        如果用户输入的内容无法解析为int(例如,您可以使用TryParse),您可能还需要包含一些错误捕获。

        拼写错误:您的数组声明错误,您需要在数组长度属性的长度上使用大写字母 L

        【讨论】:

          【解决方案5】:

          您的代码存在多个问题:

          1. 定义您的字符串数组,如string[] fruit = new string[3];
          2. 由于您在 for 循环中定义了 i,因此您需要为输入使用一个新变量。
          3. for 循环中的条件应为i &lt; fruit.Length
          4. 这更像是一个建议,使用int.TryParse 解析来自控制台的输入,检查输入的数字是否为int,并检查该数字是否小于数组长度。

          【讨论】:

            猜你喜欢
            • 2012-12-14
            • 1970-01-01
            • 2015-10-17
            • 2020-01-05
            • 1970-01-01
            • 2014-02-15
            • 2019-04-02
            • 2021-05-14
            • 1970-01-01
            相关资源
            最近更新 更多