C#:输入一个百分制的成绩t将其转换成对应的等级然后输出具体转换规则如下:90~100 为 A80~;89 为 B;70~79 为 C;60~69 为 D0~59 为 E。30分要求:如果输入数据不在0~100 范围内请输出一行:Score is error!。
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("请输入成绩:");
int score = Convert.ToInt32(Console.ReadLine());
if (score >= 0 && score <= 100)
{
string grade;
if (score >= 90)
{
grade = "A";
}
else if (score >= 80)
{
grade = "B";
}
else if (score >= 70)
{
grade = "C";
}
else if (score >= 60)
{
grade = "D";
}
else
{
grade = "E";
}
Console.WriteLine("对应的等级为:" + grade);
}
else
{
Console.WriteLine("Score is error!");
}
}
}
这段代码首先通过Console.ReadLine()方法获取用户输入的成绩,然后使用Convert.ToInt32()方法将输入的字符串转换为整数类型。接下来使用条件判断语句来判断成绩所属的等级,并将对应的等级存储在grade变量中。最后根据输入数据是否在0~100范围内,输出对应的等级或错误信息
原文地址: https://www.cveoy.top/t/topic/iJuh 著作权归作者所有。请勿转载和采集!