【发布时间】: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