我如何接受C#中的空格?
我试图创建一个程序,通过计算每门学科的分数来计算考试结果。它几乎完成了,但我遇到了一个错误,即,如果用户只是按enter键而不是输入值,应用程序将无法继续。还有没有办法缩短这个代码我如何接受C#中的空格?,c#,C#,我试图创建一个程序,通过计算每门学科的分数来计算考试结果。它几乎完成了,但我遇到了一个错误,即,如果用户只是按enter键而不是输入值,应用程序将无法继续。还有没有办法缩短这个代码 Console.WriteLine("Danyal's result calculator for students of class IX. Enter the marks requested, if not applicable leave blank and press enter :)"); Console.
Console.WriteLine("Danyal's result calculator for students of class IX. Enter the marks requested, if not applicable leave blank and press enter :)");
Console.Write("Enter your Urdu marks: ");
int urdu = int.Parse(Console.ReadLine());
Console.Write("Enter your Maths marks: ");
int maths = int.Parse(Console.ReadLine());
Console.Write("Enter your English Literature marks: ");
int lit = int.Parse(Console.ReadLine());
Console.Write("Enter your Biology marks: ");
int bio = int.Parse(Console.ReadLine());
Console.Write("Enter your Chemistry marks: ");
int chem = int.Parse(Console.ReadLine());
Console.Write("Enter your Islamiat marks: ");
int isl = int.Parse(Console.ReadLine());
Console.Write("Enter your Physics marks: ");
int physics = int.Parse(Console.ReadLine());
Console.Write("Enter your Computer marks: ");
int comp = int.Parse(Console.ReadLine());
Console.Write("Enter your English Language marks: ");
int lang = int.Parse(Console.ReadLine());
Console.Write("Enter your Pakistan Studies marks: ");
int pst = int.Parse(Console.ReadLine());
int total = urdu + maths + lit + lang + bio + chem + physics + isl + comp + pst;
Console.WriteLine("Your total marks are {0} out of 1000", total);
float percentage = total * 100 / 1000;
Console.WriteLine("Your percentage is: {0}%",percentage);
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
Console.ReadLine();
我不确定如果用户将其留空或输入无效值,您会想做什么,但您可以这样做
Dictionary<string,int> grades = new Dictionary<string, int>
{
{ "Urdu", 0 },
{ "Maths", 0 },
{ "English", 0 },
{ "Biology", 0 },
{ "Chemistry", 0 },
{ "Islamiat", 0 },
{ "Physics", 0 },
{ "Computer", 0 },
{ "English Language", 0 },
{ "Pakistan Studies", 0 },
};
foreach (string grade in grades.Keys.ToArray())
{
Console.WriteLine(string.Format("Enter your {0} marks: ", grade));
int mark;
if (int.TryParse(Console.ReadLine(), out mark))
grades[grade] = mark;
}
int total = grades.Sum((g) => g.Value);
字典等级=新字典
{
{“乌尔都语”,0},
{“数学”,0},
{“英语”,0},
{“生物学”,0},
{“化学”,0},
{“伊斯兰”,0},
{“物理学”,0},
{“计算机”,0},
{“英语”,0},
{“巴基斯坦研究”,0},
};
foreach(grades.Keys.ToArray()中的字符串级别)
{
WriteLine(string.Format(“输入你的{0}标记:”,等级));
整数标记;
if(int.TryParse(Console.ReadLine(),out mark))
成绩[等级]=分数;
}
int total=等级和((g)=>g.值);
在本例中,如果使用错误输入,则等级将默认为0。如果愿意,可以将If try解析更改为循环,并请求一个好的值,直到输入一个值为止 这显然是一个不遵守原则的例子。你在重复本质上相同的操作,不要这样做。重构代码,以便在一个地方解决常见的模式或行为 你在这里怎么做
private static int GetUserInput(string message) { ... }
private static int ValidateUserInput(string input) { ... }
这够好吗?嗯……不完全是。如果用户输入的号码不正确,会发生什么情况?没有方便的方式向调用者传达输入无效。我们可以返回-1,也可以抛出一个异常,但两者似乎都很冷漠
最好的解决方案是返回两个值;一个告诉我们输入是否有效,另一个告诉我们输入是什么。在c#中,这并不是很艰难(至少在c#7出现之前)。方法是使用out
参数:
private static bool ValidateUserInput(string message, out int input) { ... }
现在这种方法完全符合我们的目的。返回值告诉我们输入是否有效,out
参数input
为我们提供已验证的值(或者如果验证失败,我们干脆忽略它)int
变量?让我们创建一个列表
,存储所有标记
另一个选项是,如果您想跟踪哪个标记对应于哪个主题,可以使用字典,其中键是主题名称及其对应标记的值。但是现在让我们使用List
public static void ComputeMarksSummary()
{
var marks = new List<int>();
marks.Add(GetUserInput("Enter your Urdu marks: "));
marks.Add(GetUserInput("Enter your Maths marks: : "));
marks.Add(GetUserInput("Enter your English Literature marks: "));
marks.Add(GetUserInput("Enter your Biology marks: "));
marks.Add(GetUserInput("Enter your Chemistry marks: "));
marks.Add(GetUserInput("Enter your Islamiat marks: "));
marks.Add(GetUserInput("Enter your Computer marks: "));
marks.Add(GetUserInput("Enter your English language marks: "));
marks.Add(GetUserInput("Enter your Pakistan studies marks: "));
var total = marks.Sum();
Console.WriteLine("Your total marks are {0} out of 1000", total);
Console.WriteLine("Your percentage is: {0}%", total/10.0); //note the 10.0 to force double division, not integer division where 44/10 would be 4 not 4.4
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
//WRONG! Percentage is not rounded off, its truncated: 9/10 is 0 in integer division.
Console.ReadLine();
}
private static int GetUserInput(string message)
{
int mark;
while (true)
{
Console.WriteLine(message);
var input = Console.ReadLine();
if (!ValidateUserInput(input, out mark))
{
Console.WriteLine("Invalid input, please try again.");
}
else
{
return mark;
}
}
}
private static bool ValidateUserInput(string message, out int input)
{
//left as an excerice. Hint: look into int.TryParse(...);
//here you could decide if a blank input should be valid and parsed as zero.
}
你可以这样称呼它:
public static void ComputeMarksSummary(IEnumerable<string> subjectNames)
{
var marks = new List<int>();
foreach (var subject in sujectNames)
{
marks.Add(GetUserInput(string.Format("Enter your {0} marks: ", subject)));
}
var total = marks.Sum();
Console.WriteLine("Your total marks are {0} out of 1000", total);
Console.WriteLine("Your percentage is: {0}%", total/10.0); //note the 10.0 to force double division, not integer division where 44/10 would be 4 not 4.4
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
Console.ReadLine();
}
ComputeMarksSummary(new string[] { "Urdu", "Maths", ... );
这看起来不是很干净吗?就像他们输入了任何不是数字的东西一样?创建一个方法,在loopWoah中提示并调用int.TryParse()!这是很难吸收的。你看,我是一个完全的新手刚刚开始,我用我的想象力。我在微软虚拟学院学习。非常感谢!写下所有这些并解释。这对我很有帮助,我也知道了不同的帮助方法,比如.sum和抱歉使用了错误的术语,使事情变得圆满。@DanyalAhmedKhan不要绝望!我明白,对于初学者来说,这似乎有点让人望而生畏,但归根结底,这只是应用逻辑,思考如何以更清晰、更易于维护的方式编写代码……记住,专业程序员的大部分时间不是花在编写代码上,而是花在审查其他人上。想象一下,明天您必须改进代码,添加两个新主题,删除一个不推荐的主题。想想你需要如何修改你的代码,以及你需要如何修改我答案的最终版本。让你和其他人的未来生活更轻松!我遇到了另一个问题@希望你也能帮我