[英]C# How can I make this code better or shorter?
所以我基本上只是开始学习计算机编程(C#),今天我开始学习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,这就是为什么我要就如何使此代码变得更好或更短寻求一些反馈,我敢肯定, Arrays必须有一种方法可以存储所有这三个代码我在代码中使用的名字,并给他们对应的分数,是的,我喜欢反馈,谢谢! 也要尝试不要回答疯狂的答案或我想太多的细节,就像我说过我刚开始学习C#一样,因此请尝试使其尽可能简单,以便更好地理解(如果可以或想要)。
PD:对不起,我的语法不好,英语不是我的母语。
您可以创建一个代表学生信息的班级,例如姓名和年级。 将学生信息存储在学生对象列表中。 使用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; }
}
通常的规则是永远不要一遍又一遍地复制和粘贴类似的代码 。 这就是数组和循环存在的原因。 将学生排列成阵列,并浏览他们以查找匹配项
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]);
}
}
你可以试试这个。
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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.