gpt4 book ai didi

c# - 如何将字母转换为数字并打印出 BTEC 最终成绩

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:08 28 4
gpt4 key购买 nike

我正在为那些不了解的人设计一个 BTEC 成绩计算器; BTEC 是在英国完成的一种资格认证,具有主要基于类(class)作业的“不同”评分系统。有许多单元 (20),每个单元您可以获得每个单元的及格、优点和区别。每一次通过、优点和区别加起来这么多分,这些分加起来就是总成绩。

例如,120 分将映射到 PPP 等级,240 分将映射到 MMM 等等。

我已经根据以下伪代码编写了一个程序:

Prompt for student name:
Operator enters student name
Operator enters their grade units one at a time (D, M, P, D etc):
Display Message about final grade:
Your final Grade is:
DMM

以下是我当前的代码,但需要更改和修复。目前,我需要以数字 (20, 40) 的形式输入单元成绩。我希望能够输入等级字母 (D、M、P)。如何更改我的程序,以便它可以解释字母并将它们映射到单元分数?

我还遇到了从最终分数转换为最终成绩的问题。目前,如果我输入两个单元成绩 80,总分 160,显示的成绩应该是 MPP,但它打印的是 120。我做错了什么?

class Program
{
static void Main(string[] args)
{
int counter;
double score;

while (true)
{
counter = 1;
score = 0.0;
Console.WriteLine("Enter Your Name or type 'exit' to quit): ");
// Naming for exiting
string name = Console.ReadLine();
if (name == "exit")
{
Environment.Exit(0);
}
else
{
int numberOfUnits = 20;
Console.WriteLine("Number of Units: ");
numberOfUnits = int.Parse(Console.ReadLine());

while (counter <= numberOfUnits)
{
Console.WriteLine("Final Grade Score {0}", counter);
score += double.Parse(Console.ReadLine());
counter++;
}
// Add up and print letter grade.
score = (score / (counter - 1));
if (score < 120)
{
Console.WriteLine("Letter Grade: PPP");
}
else if (score < 160)
{
Console.WriteLine("Letter Grade: MPP");
}
else if (score < 200)
{
Console.WriteLine("Letter Grade: MMP");
}
else if (score < 240)
{
Console.WriteLine("Letter Grade: MMM");
}
else if (score < 280)
{
Console.WriteLine("Letter Grade: DMM");
}
else if (score < 320)
{
Console.WriteLine("Letter Grade: DDM");
}
else if (score < 360)
{
Console.WriteLine("Letter Grade: DDD");
}
// After DDD it goes up by 20's ..... D* = 20 not 40...
else if (score < 380)
{
Console.WriteLine("Letter Grade: D*DD");
}
else if (score < 400)
{
Console.WriteLine("Letter Grade: D*D*D");
}
else if (score < 420)
{
Console.WriteLine("Letter Grade: D*D*D*");
}
Console.WriteLine("Grade: {0}", (score).ToString("P"));
}
}
}
}

最佳答案

这是经过一些小改动的代码,以及将字母转换为数字的 switch 语句。

public class Program
{
static void Main(string[] args)
{
int counter;
double score;

while (true)
{
counter = 1;
score = 0.0;
Console.WriteLine("Enter Your Name or type 'exit' to quit): ");
// Naming for exiting
string name = Console.ReadLine();
if (name == "exit")
{
return;
}
else
{
int numberOfUnits = 20;
Console.WriteLine("Number of Units: ");
numberOfUnits = int.Parse(Console.ReadLine());

while (counter <= numberOfUnits)
{
Console.WriteLine("Final Grade Score {0}", counter);
string s = Console.ReadLine().ToUpper();
double d;
if (double.TryParse(s, out d)) //parsing as a number was successfull, and the result is in d.
score += d;
else //parsing failed.
{
switch (s)
{
case "D":
score += 20;
break;
case "M":
score += 50;
break;
case "P":
score += 70;
break;
default:
counter--; //parsing of a letter failed. reducing counter that will be later incremented, to offset the effect.
Console.WriteLine("Could not parse your grade. Enter a number or a letter");
break;
}
}
counter++;
}
// Add up and print letter grade.
score = (score / (counter - 1));

if (score < 120) Console.WriteLine("Letter Grade: PPP");
else if (score < 160) Console.WriteLine("Letter Grade: MPP");
else if (score < 200) Console.WriteLine("Letter Grade: MMP");
else if (score < 240) Console.WriteLine("Letter Grade: MMM");
else if (score < 280) Console.WriteLine("Letter Grade: DMM");
else if (score < 320) Console.WriteLine("Letter Grade: DDM");
else if (score < 360) Console.WriteLine("Letter Grade: DDD");
// After DDD it goes up by 20's ..... D* = 20 not 40...
else if (score < 380) Console.WriteLine("Letter Grade: D*DD");
else if (score < 400) Console.WriteLine("Letter Grade: D*D*D");
else if (score < 420) Console.WriteLine("Letter Grade: D*D*D*");
Console.WriteLine("Grade: {0}", (score).ToString("P"));
}
}
}
}

关于c# - 如何将字母转换为数字并打印出 BTEC 最终成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30529274/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com