索引超出我的值的范围
本文关键字:范围 我的 索引 | 更新日期: 2023-09-27 18:36:03
我正在尝试打印时间'NONE出现,因为我的所有其他信息打印准确。我相信这可能是我正在用我的价值观做的事情,但每次我修复某些东西并让我的代码编译时,我总是得到零作为我的答案。这是为什么呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication33
{
class Program
{
static void Main(string[] args)
{
FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;
double GPA;
double total = 0;
double counter = 0;
double count = 0;
double counti = 0;
List<string> Anderson = new List<string>(); //Anderson
List<string> gpa = new List<string>(); //GPA
List<string> noemail = new List<string>(); // email
while (!inFile.EndOfStream)
{
inValue = inFile.ReadLine();
values = inValue.Split(" ".ToCharArray());
if (inValue.StartsWith("(LIST (LIST "))
{
values = inValue.Split(" ".ToCharArray());
GPA = double.Parse(values[8]);
total = total + GPA;
counter++;
}
if (values[2] == "'Anderson")
{
Anderson.Add(values[2]);
count++;
}
if (values[2] == " ")
{
counter++;
}
if (values[6] == "'NONE")
{
noemail.Add(values[6]);
counti++;
}
}
double average = (double)total / (double)counter;
Console.WriteLine("The average gpa is..." + average.ToString());
Console.WriteLine("Total last names with Anderson is..." + count.ToString());
Console.WriteLine("Total number of students is..." + counter.ToString());
Console.WriteLine("Total is..." + counti.ToString());
}
}
}
当试图确定电子邮件是否存在时,您与一个人相差
FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;
double GPA;
double total = 0;
double counter = 0;
double count = 0;
double counti = 0;
double countNoTelephone = 0;
List<string> Anderson = new List<string>(); //Anderson
List<string> gpa = new List<string>(); //GPA
List<string> noemail = new List<string>(); // email
while (!inFile.EndOfStream)
{
inValue = inFile.ReadLine();
if (inValue.StartsWith("(LIST (LIST "))
{
values = inValue.Split(" ".ToCharArray());
GPA = double.Parse(values[8]);
total = total + GPA;
counter++;
if (values[2] == "'Anderson")
{
Anderson.Add(values[2]);
count++;
}
if (values[2] == " ")
{
counter++;
}
if (values[7] == "'NONE") // you got this off by one
{
noemail.Add(values[6]);
counti++;
}
if (values[6] == "'NONE") // you are not counting this
{
countNoTelephone++;
}
}
}
double average = total / counter;
Console.WriteLine("The average gpa is..." + average);
Console.WriteLine("Total last names with Anderson is..." + count);
Console.WriteLine("Total number of students is..." + counter);
Console.WriteLine("Total with no emails is..." + counti);
Console.WriteLine("Total with no telephone is..." + countNoTelephone);
Console.WriteLine("Total with no telephone or emails are..." + (countNoTelephone + counti)); // add both no telephone and no emails together
Console.ReadKey();
执行此操作的更好方法是将文本文件序列化为 .NET 对象,然后对其执行计算。看看这个
public class Student
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string Code { get; set; } // not sure what the J M T K code is
public string Telephone { get; set; }
public string Email { get; set; }
public double Gpa { get; set; }
}
private static void Main(string[] args)
{
FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;
List<Student> students = new List<Student>();
while (!inFile.EndOfStream)
{
inValue = inFile.ReadLine();
if (inValue.StartsWith("(LIST (LIST "))
{
values = inValue.Split(" ".ToCharArray());
Student student = new Student();
student.LastName = values[2];
student.FirstName = values[3];
student.Code = values[4];
student.Telephone = values[6];
student.Email = values[7];
student.Gpa = Convert.ToDouble(values[8]);
students.Add(student);
}
}
var noTelephone = students.Count(x => x.Telephone == "'NONE");
int noEmails = students.Count(x => x.Email == "'NONE");
Console.WriteLine("The average gpa is..." + students.Average(x => x.Gpa));
Console.WriteLine("Total last names with Anderson is..." + students.Count(x => x.LastName == "'Anderson"));
Console.WriteLine("Total number of students is..." + students.Count);
Console.WriteLine("Total with no emails is..." + noEmails);
Console.WriteLine("Total with no telephone is..." + noTelephone);
Console.WriteLine("Total with no telephone or emails are..." + (noEmails + noTelephone));
Console.ReadKey();
}
}
之后,您可以轻松地对数据执行其他操作,并且您有一个很好的对象可以使用,而不是猜测值[7]是什么。
我不确定你在追求什么,但如果你想计算文件中出现"NONE"的次数,这应该可以工作:
while (!inFile.EndOfStream)
{
inValue = inFile.ReadLine()
myCount = System.Text.RegularExpressions.Regex.Matches(inValue, "NONE").Count;
}
Console.WriteLine("NONE occurs {0} time(s).", myCount);
当您将输入文件的第一行拆分为 " "
时:
students (LIST
您只会得到 2 个分隔值,当您尝试访问更多值时会导致索引超出范围异常,例如
if (values[2] == "'Anderson")
尝试访问可用分隔值之前,您必须检查它们的数量。