C#如何使这段代码更好或更短?

时间:2017-02-28 04:20:16

标签: c#

所以我基本上刚开始学习计算机编程(C#),今天我学习了 Arrays 。我有点,有点理解 Arrays 是什么,我知道它们用于跟踪列表或许多相关事物的集合。所以我想制作一个程序,要求用户提供他的名字,在你输入你的名字之后,它会自动给你你得到的分数"测试"所以我想向你们展示我的代码,并且我会喜欢一些关于如何使我的代码看起来更好更短的反馈,我想......无论如何我的代码都是。

        string studentNumberOne = "Miguel"; 
        string studentNumberTwo = "Maddie";
        string studentNumberThree = "John";
        string studentName = ""; // Empty string variable for user input...


        int[] grades = new int[3] { 90, 85, 70 }; // arrays, grades for each students...

        Console.WriteLine("Please enter your name: "); // Asking the user to type his/her name
        studentName = Convert.ToString(Console.ReadLine()); //


        if (studentName == studentNumberOne) 
        {
            Console.WriteLine(studentNumberOne + " your score was " + grades[0]);

        }

        else if (studentName == studentNumberTwo)
        {
            Console.WriteLine(studentNumberTwo + " your score was " + grades[1]);

        }

        else if (studentName == studentNumberThree)
        {
            Console.WriteLine(studentNumberThree +" your score was " + grades[2]);
        }



    }
}

我知道那里有很多if / else if,以及为什么我要求对如何使这段代码更好或更短的一些反馈,我&#39 ;我非常肯定必须有一种方法可以使用数组,你可以在我的代码中存储我拥有的所有三个名字,并给他们相应的分数,所以是的,我会喜欢反馈,谢谢!我也想尝试不要疯狂的回答或过多的细节,就像我说我刚刚开始学习C#所以尽量让它变得简单,这样我就能更好地理解(如果你能或想要的话)。

PD:抱歉我的语法不好,英语不是我的第一语言。

3 个答案:

答案 0 :(得分:2)

您可以创建一个表示学生信息的课程,例如姓名和成绩。将学生信息存储在学生对象列表中。使用LINQ根据学生姓名过滤列表。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<Student> students = new List<Student>
        {
            new Student {Name = "Miguel", Grades = 90},
            new Student {Name = "Maddie", Grades = 70},
            new Student {Name = "John", Grades = 65},
            new Student {Name = "Isabella", Grades = 80},
            new Student {Name = "Raul", Grades = 75}
        };

        Console.WriteLine("Please enter your name: "); // Asking the user to type his/her name
        string studentName = Console.ReadLine(); //

        var student = students.FirstOrDefault(std => std.Name == studentName);
        if (student != null)
        {
            Console.WriteLine(studentName + " your score was " + student.Grades);
        }
    }
}

public class Student
{
    public string Name { get; set; }

    public int Grades { get; set; }
}

答案 1 :(得分:1)

一般规则是永远不要一遍又一遍地复制和粘贴类似的代码。这就是数组和循环存在的原因。将学生放在一个数组中并查看它们以找到匹配

class Program
{
    static void Main(string[] args)
    {
        string[] students=new string[] {
            "Miguel", "Maddie", "John", "Isabella", "Raul" };

        int[] grades=new int[] { 
            90, 85, 70, 92, 87 }; // arrays, grades for each students...

        Console.WriteLine("Please enter your name: "); // Asking the user to type his/her name
        string studentName=Console.ReadLine(); //

        for (int i=0; i<students.Length; i++)
        {
            if (students[i].Equals(studentName))
            {
                Console.WriteLine(studentName+" your score was "+grades[i].ToString());
            }
        }

    }
}

修改1

当然,CRL有一个Dictionary对象,用于将名称与成绩连接起来。这比维护两个单独的阵列更有效和灵活。见下面的例子:

    static void Main(string[] args)
    {
        Dictionary<string, int> grades=new Dictionary<string, int>();
        grades["Miguel"] = 90;
        grades["Maddie"] = 85;
        grades["John"] = 70;
        grades["Isabella"] = 92;
        grades["Raul"] = 87;

        Console.WriteLine("Please enter your name: "); // Asking the user to type his/her name
        string studentName=Console.ReadLine(); //

        if (grades.ContainsKey(studentName))
        {
            Console.WriteLine(studentName+" your score was "+grades[studentName]);
        }
    }

答案 2 :(得分:0)

你可以试试这个。

using System;
using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        List<string> students = new List<string> {
            "Miguel", "Maddie", "John", "Isabella", "Raul" };

        int[] grades=new int[] { 
            90, 85, 70, 92, 87 }; // arrays, grades for each students...

        Console.WriteLine("Please enter your name: "); // Asking the user to type his/her name
        string studentName=Console.ReadLine();
        try{
          Console.WriteLine(studentName+" your score was "+grades[students.IndexOf(studentName)].ToString());
          }
          catch(Exception){
            Console.WriteLine("name Not found");
          }
    }
}

如果您需要更准确的错误跟踪,可以将例外更改为IndexOutOfRangeException