2013-10-04 32 views
1

有人可以看我的代碼嗎?我如何計算循環中的成績,或者我可以從StreamWriter中取出這一行並放入子字符串? 我一直讓它越來越雜亂......我需要像通過使用Streamwriter中的答案循環計數等級?

那樣顯示它那個得分A的學生有多少? 學生,進行了B獲得了多少?> 學生,進行了ç多少> 學生,進行了d ... F ...

using (StreamReader reader = new StreamReader("Student.txt")) 
{ 
    StreamWriter output = new StreamWriter("Result.txt"); 
    String line; 
    line = reader.ReadLine(); 

    while (line != null) 
    { 

     String name = line.Substring(0, 9); 
     String Asg1 = line.Substring(10, 3); 
     String Asg2 = line.Substring(16, 3); 
     String Test = line.Substring(22, 3); 
     String Quiz = line.Substring(28, 3); 
     String Exam = line.Substring(34, 3); 

     int intAsg1 = int.Parse(Asg1); 
     int intAsg2 = int.Parse(Asg2); 
     int intTest = int.Parse(Test); 
     int intQuiz = int.Parse(Quiz); 
     int intExam = int.Parse(Exam); 

     int percentAsg1 = (intAsg1 * 25)/100; 
     int percentAsg2 = (intAsg2 * 25)/100; 
     int percentTest = (intTest * 10)/100; 
     int percentQuiz = (intQuiz * 10)/100; 
     int percentExam = (intExam * 30)/100; 


     // this part i dont get it~ 
     **String Grade; 
     if (overallScore >= 70) 
     { 
      Grade = "A"; 
     } 
     else if (overallScore >= 60) 
     { 
      Grade = "B"; 
     } 
     else if (overallScore >= 50) 
     { 
      Grade = "C"; 
     } 
     else if (overallScore >= 40) 
     { 
      Grade = "D"; 
     } 
     else 
     { 
      Grade = "F"; 
     } 
    } 
} 
+0

爲了得到最好的答案,你可以從文件 –

+1

可以發佈一個樣本行之前創建一個列表你試着讓你的問題更清楚一點? – Rohan

+0

我得分後我想把它放在數組或什麼,這樣我就可以計算出「成績」有多少學生獲得 – user2844803

回答

1

這不是循環。

while !reader.EndOfStream 
{ 
    /*now check each Readline. 
    parse each line and create a new grade object for 
    each iteration and add to collection*/ 
} 

一個自定義類來存儲數據爲每個學生:

public class grade 
{ 
public int asg1{ get; set; } 
public int asg2{ get; set; } 
public int test{ get; set; } 
public int quiz{ get; set; } 
public int exam{ get; set; } 
//whatever else you need 
} 

等級的集合:

var grades = New List<grade> 
+0

那我該如何算總分?在那裏 – user2844803

+0

更新........ – OneFineDay

0

我認爲你正在尋找一個類來保存結果除了糾正readline循環。

public class Student 
{ 
public string Name { get; set; } 
public List<Grade> Grades { get; set; } 
} 

然後就開始循環

var students = new List<Student>(); 

然後添加到列表中的每個迭代