我是C#的初学者,当我遇到这个问题时,我正在尝试使用类和数据结构。我想使用class char给出char输入来从它的枚举描述中获取int输出。欢迎任何帮助。感谢
using System;
namespace dataStorageOfFiveStudents
{
public class StudentData
{
public string name;
public int rollNo;
public char grade;
public float gpa;
}
public class Program
{
enum grading { A=4, B=3, C=2, D=2, F=0};
static void Main(string[] args)
{
//Data Entry
StudentData student1 = new StudentData();
Console.WriteLine("name?");
student1.name = Console.ReadLine();
Console.WriteLine("Roll Number?");
student1.rollNo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Grade");
student1.grade = Convert.ToChar(Console.ReadLine());
student1.gpa = (int)grading.student1.grade;
//Output
Console.WriteLine("Name =\t" + student1.name);
Console.WriteLine("Roll No =\t" + student1.rollNo);
Console.WriteLine("Grade =\t" + student1.grade);
Console.WriteLine("GPA =\t" + student1.gpa);
Console.ReadKey();
}
}
}
答案 0 :(得分:2)
您应该使用Enum.Parse或Enum.TryParse。
grading gpa;
if (Enum.TryParse(student1.grade.ToString(), true, out gpa))
{
student1.gpa = (float)(int)gpa;
Console.WriteLine("Name =\t" + student1.name);
Console.WriteLine("Roll No =\t" + student1.rollNo);
Console.WriteLine("Grade =\t" + student1.grade);
Console.WriteLine("GPA =\t" + student1.gpa);
}
else
{
Console.WriteLine("You entered an invalid letter grade");
}
答案 1 :(得分:0)
使用Enum.Parse
假设:
public class StudenData {
public string name { get;set; }
public int rollNo {get;set; }
public string grade {
get { return gpaEnum.ToString(); }
set { gpaEnum = (grading)Enum.Parse(typeof(grading),value); }
public gpaEnum { get;set; }
public int gpa {
get { return (int)gpaEnum; }
set { gpaEnum=(grading)value; }
}
}
然后
//Data Entry
StudentData student1 = new StudentData();
Console.WriteLine("name?");
student1.name = Console.ReadLine();
Console.WriteLine("Roll Number?");
student1.rollNo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Grade");
student1.grade = Console.ReadLine();
//Output
Console.WriteLine("Name =\t" + student1.name);
Console.WriteLine("Roll No =\t" + student1.rollNo);
Console.WriteLine("Grade =\t" + student1.grade);
Console.WriteLine("GPA =\t" + student1.gpa);
Console.ReadKey();
编辑---由于数据结构似乎固定为
public class StudenData {
public string name { get;set; }
public int rollNo {get;set; }
public char grade { get;set; }
public float gpa { get;set; }
}
然后
//Data Entry
StudentData student1 = new StudentData();
Console.WriteLine("name?");
student1.name = Console.ReadLine();
Console.WriteLine("Roll Number?");
student1.rollNo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Grade");
grading grade = (grading)Enum.Parse(typeof(grading),Console.ReadLine());
student1.grade = grade.ToString()[0]; // Grab first char of grade.
student1.gpa = (float)(int)grade;
//Output
Console.WriteLine("Name =\t" + student1.name);
Console.WriteLine("Roll No =\t" + student1.rollNo);
Console.WriteLine("Grade =\t" + student1.grade);
Console.WriteLine("GPA =\t" + student1.gpa);
Console.ReadKey();
此代码没有错误检查,如果要添加验证检查而不是失败的解析异常,则可以使用Enum.TryParse
答案 2 :(得分:0)
您可以使用此代码轻松完成
student1.gpa = (int)(Enum.Parse(typeof(grading), student1.grade.ToString().ToUpper()));