【问题标题】:How do I convert string[] to int[]? [duplicate]如何将 string[] 转换为 int[]? [复制]
【发布时间】:2014-12-12 04:02:20
【问题描述】:

如何将字符串数组转换为整数数组?由于这个小问题,我不会让我运行程序。任何想法如何做到这一点?我必须从文件中提取数字列表,将它们读取到控制台,然后获取平均数字并将其显示给用户。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace lab25LEA
{
    class Program
    {
        const int MAX = 50; 

        static void Main(string[] args)
        {
            // In your program create an array of 50 integers
            // to hold the data that comes from the file.
            int[] numbers = new int[MAX];

            // Your program must get the path to the user's Documents folder as described in the reading material on File Paths.
            // The name of the file will be "grades.txt". Code this file name right in your program. 
            // No user input is required to get the file name.

            // Create a StreamReader object, 
            // using this path. This will open the file.
            StreamReader data = new StreamReader("grades.txt");
            string fromFile;
            int count = 0;

            // Write a loop that reads data from the file, 
            // until it discovers the end of the file.
            do
            {
                fromFile = data.ReadLine();
                // As each integer value is read in, display it, and store it in the array.
                if (fromFile != null)
                {
                    // Using the concepts taught earlier about partially filled arrays, 
                    // write a method that takes the array as a parameter and calculates and returns the average 
                    // value of the integers stored in the array
                    int[] dataArray = fromFile.Split();

                    numbers[count] = dataArray[1];
                    count++;
                }
            } while (fromFile != null);

            // Output the average.
            AverageScore(numbers);

            Console.ReadLine();
        }

        static void AverageScore(int[] numbers)
        {
            int sum = numbers.Sum();
            int average = sum / numbers.Length;
        }
    }
}

【问题讨论】:

    标签: c# type-conversion


    【解决方案1】:

    只需使用Select 将每个元素转换为等效整数:

    string[] strArray = fromFile.Split();
    int[] dataArray = strArray.Select(int.Parse).ToArray();
    

    【讨论】:

      【解决方案2】:

      试试这个

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.IO;
      
      namespace lab25LEA
      {
          class Program
          {
              const int MAX = 50; 
      
              static void Main(string[] args)
              {
                  // In your program create an array of 50 integers
                  // to hold the data that comes from the file.
                  int[] numbers = new int[MAX];
      
                  // Your program must get the path to the user's Documents folder as described in the reading material on File Paths.
                  // The name of the file will be "grades.txt". Code this file name right in your program. 
                  // No user input is required to get the file name.
      
                  // Create a StreamReader object, 
                  // using this path. This will open the file.
                  StreamReader data = new StreamReader("grades.txt");
                  string fromFile;
                  int count = 0;
      
                  // Write a loop that reads data from the file, 
                  // until it discovers the end of the file.
                  fromFile = data.ReadLine();
      
                  while (fromFile != null){
      
                      // As each integer value is read in, display it, and store it in the array.
                      if (fromFile != null)
                      {
                          // Using the concepts taught earlier about partially filled arrays, 
                          // write a method that takes the array as a parameter and calculates and returns the average 
                          // value of the integers stored in the array
                          int[] dataArray = fromFile.Split(" ");
      
                          numbers[count] = Convert.ToInt32(dataArray[1]);
                          count++;
      
                          fromFile = data.ReadLine();
                      }
                  }
      
                  // Output the average.
                  AverageScore(numbers);
      
                  Console.ReadLine();
              }
      
              static void AverageScore(int[] numbers)
              {
                  int sum=0;
                  int average=0;
                  if(numbers.length > 0){
                       sum = numbers.Sum();
                       average = sum / numbers.Length;
                  }
      
                  Console.WriteLine("Average is :{0}",average);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-28
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 2010-11-04
        • 1970-01-01
        • 2019-07-28
        • 2012-11-22
        相关资源
        最近更新 更多