2014-02-18 41 views
0

此代码工作正常,但没有用于存储用户输入的内存。将用户输入存储到数组中并引用数组c#

我还需要将等级分为数组格式的相应列,具有“S/N,类别和计数”,我不知道如何去做。任何帮助,将不胜感激。

namespace ExamScore 
{  
    class YourExamScore 
    {  
     private static string GetGrade(int examScore) 
     { 
      if (examScore >= 90 && examScore <= 100) 
       return "Excellent"; 

      if (examScore >= 70 && examScore <= 89) 
       return "Good"; 

      if (examScore >= 50 && examScore <= 69) 
       return "Satisfactory"; 

      if (examScore >= 0 && examScore <= 49) 
       return "Unsatisfactory"; 

      return "Invalid";  
     }  

     static void Main(string[] args) 
     { 
      // Print a greeting message. After all, why not? 
      Console.WriteLine("Welcome to ExamScore Calculator!"); 

      Console.WriteLine("Input Your Exam Score..."); 
      Console.WriteLine("Press -2 when you have inputed all scores"); 
      while (true) 
      { 
       var examScore = Convert.ToInt32(Console.ReadLine()); 
       if (examScore == -2) 
       { 
        break; 
       } 

       var grade = GetGrade(examScore); 
       Console.WriteLine(grade); 
      } 
      Console.WriteLine("\n\nProccessing Scores... Please Wait..."); 
      Console.ReadLine(); 
     } 

    }   
} 
+1

这功课吗?如果是这样,请添加“家庭作业”标签。 –

+0

没有它不是家庭作业,我为这家新公司工作,但正在接受C#培训因为我最近需要它很多 – user3282967

回答

0

那么它似乎是一个家庭作业,所以我不打算帮助直接编码,

为标志的存储,可以遵循的一种方式

  1. 如果你知道你要使用多少输入,然后使用固定大小array

    var marks = new int [10];

  2. 如果没有。的输入是不固定的,那么你可以使用List<int>

    var marks = new List();

+0

它不是家庭作业,即使它处理学生的工作,但我感谢您的回应。感谢 – user3282967

0

我不确定我是否正确理解了你,但我想你是要求跟踪属于某个类别的成绩的数量。在那种情况下,我会这样做。

您将需要跟踪一系列整数(int[]),并且每个类别都有一个条目,用于跟踪目前为止该类别中的成绩数量。所以你首先必须创建一个大小为4的静态数组,它会自动初始化为零。在语义上,每个索引代表一个类别(比如0表示优秀,1表示优秀等)。然后,在GetGrade方法中,您应该为每个类别在返回之前也增加正确的类别。

+0

S/N类别计数 1极好5 2令人满意8 等这种格式是什么我试图实现 – user3282967

0

存储用户输入的内存,你可以使用数组或列表(List<MyData>是更好,如果你有,如果你要搜索快速插入频繁,HashtableMyData[] - 内存最少,并通过指数最快的访问等)

输入-2之后,您可能想要以某种方式存储(序列化)您的数据,因此数据将在运行之间持续存在。它可以是xml文件(XmlSerializer),自己的格式文本/二进制文件,数据库等。

您还需要定义如何使用数据:它们是否在输入后立即保存(然后数据库本身是一个存储器)或退出时,删除/更正可能性等。

最后,您可以添加统计和报告,其中查询您的数据并产生结果。

这是一个有点不清楚

“S/N,分类和计数”

代表。你能详细说明一下吗?您的代码是要求score和生产grade。你可以指望有多少用户具有哪个等级,多少个总用户,多少等级(所有这些都是“计数”),但是“S/N”和“类别”是混淆的。


例如,您只想打印有多少用户属于不同等级。

1)非常的手工做法,在进入(使用)

int grade90plus; // fields 
int grade70plus; 
... 
    // inside GetGrade 
    if(examScore > 90) 
    { 
     grade90plus++; 
     return "Excellent"; 
    } 
    else 
     if(examScore > 70) 
     { 
      grade70plus++; 
      return "Good"; 
     } 
... 
// report 
Console.WriteLine("Excellent: " + grade90plus); 
Console.WriteLine("Good: " + grade90plus); 

2)High tec(系列化不友好)计数出现(统计)

public class Grade 
{ 
    public string Name; 
    public int Count; 
    public int ScoreMin; 
    public int ScoreMax; 

    public bool Test(int score) { return score >= ScoreMin && score <= ScoreMax; } 

    public static readonly Grade Excellent = new Grade() {Name = "Excellent", ScoreMin = 90, ScoreMax = 100}; 
    public static readonly Grade Good = new Grade() {Name = "Good", ScoreMin = 70, ScoreMax = 89}; 

    public static readonly Grade[] GetAll = new Grade[] { Excellent, Good }; 
} 

private static string GetGrade(int examScore) 
{ 
    foreach(var grade in Grade.GetAll) 
     if(grade.Test) 
     { 
      grade.Count++; 
      return grade.Name; 
     } 
    return "Invalid"; 
} 

// report 
foreach(var grade in Grade.GetAll) 
    Console.WriteLine(grade.Name + "," + grade.Count); 

你一定能改善这些或使自己的解决方案(序列化友好,利用数据库,一个使用Test等的表达式)。

+0

S/N类别计数 1极好5 2令人满意8 等这种格式是我试图实现 – user3282967

+0

仍然不完全适合。你知道什么'S/N'代表*序列号*? = D – Sinatr

+0

是序列号应该是阵列中的另一列,以便于清晰起见,同时将类别和数目排列在二维阵列中,但如果您觉得没有用,我可以将其忽略 – user3282967

相关问题