0

我正在尝试打印“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());
    }
}
}
4

3 回答 3

1

当您试图确定电子邮件是否存在时,您错了

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] 是什么。

于 2013-10-18T02:52:55.100 回答
1

我不确定你在追求什么,但如果你想计算文件中出现“NONE”的次数,这应该可以:

while (!inFile.EndOfStream)
    {
        inValue = inFile.ReadLine()
        myCount = System.Text.RegularExpressions.Regex.Matches(inValue, "NONE").Count;

     }
    Console.WriteLine("NONE occurs {0} time(s).", myCount);
于 2013-10-18T03:26:02.597 回答
0

当您将输入文件的第一行拆分为" "

students (LIST

您只会得到 2 个分隔值,当您尝试访问更多值时会导致 Index out of Range 异常,例如

if (values[2] == "'Anderson")

在尝试访问它们之前,您必须检查可用分隔值的数量。

于 2013-10-18T03:23:07.843 回答