2017-03-07 28 views
1

剛開始學習C#,我的問題是如何保持用戶輸入的記錄,以這樣的:score 1:我如何爲每個用戶輸入號碼? C#

score 1: 98 
score 2: 76 
score 3: 65 
score 4: 78 
score 5: 56 

在我的代碼可以輸入號碼,但不能似乎設置豈能順序我實現這個目標 我輸入:

98 
76 
65 
78 
56 

代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace MyGrade03 
{ 
    public class Program 
    { 
     private int total; // sum of grades 
     private int gradeCounter; //number of grades entered 
     private int aCount; // Count of A grades 
     private int bCount; // Count of B grades 
     private int cCount; // Count of C grades 
     private int dCount; // Count of D grades 
     private int fCount; // Count of F grades 
     private string v; 



     public string CourseName { get; set; } 

     public Program(string name) 
     { 
      CourseName = name; 
     } 

     public void DisplayMessage() 
     { 
      Console.WriteLine("Welcome to the grade book for \n{0}!\n", 
       CourseName); 
     } 

     public void InputGrade() 
     { 
      int grade; 
      string input; 

      Console.WriteLine("{0}\n{1}", 
       "Enter the integer grades in the range 0-100", 
       "Type <Ctrl> z and press Enter to terminate input:"); 

      input = Console.ReadLine(); //read user input 

      while (input != null) 
      { 
       grade = Convert.ToInt32(input); //read grade off user input 
       total += grade;// add grade to total 
       gradeCounter++; // increment number of grades 

       IncrementLetterGradeCounter(grade); 

       input = Console.ReadLine(); 
      } 
     } 
     private void IncrementLetterGradeCounter(int grade) 

     { 
      switch (grade/10) 
      { 
       case 9: //grade was in the 90s 
       case 10: 
        ++aCount; 
        break; 
       case 8: 
        ++bCount; 
        break; 
       case7: 
        ++cCount; 
       case6: 
        ++dCount; 
        break; 
       default: 
        ++fCount; 
        break; 

      } 
     } 
     public void DisplayGradeReport() 
     { 
      Console.WriteLine("\nGrade Report"); 

      if (gradeCounter != 0) 
      { 
       double average = (double)total/gradeCounter; 

       Console.WriteLine("Total of the {0} grades entered is {1}", 
        gradeCounter, total); 
       Console.WriteLine("class average is {0:F}", average); 
       Console.WriteLine("{0}A: {1}\nB: {2}\nC: {3}\nD: {4}\nF: {5} ", 
        "Number of students who received each grade: \n", 
        aCount, 
        bCount, 
        cCount, 
        dCount, 
        fCount); 
      } 
      else 
       Console.WriteLine("No grades were entered"); 
     } 
     static void Main(string[] args) 
     { 
      Program mygradebook = new Program(
       "CS101 introduction to C3 programming"); 
      mygradebook.DisplayMessage(); 
      mygradebook.InputGrade(); 
      mygradebook.DisplayGradeReport(); 
     } 
    } 
} 
+0

你不是你想要什麼明確的。做一點研究和努力,特別是在這個學校任務。 – EpicKip

+0

有什麼你想要做的輸入序數?它只是一個索引,還是會用於任何事情? – Dave

+0

@dave我只想知道我該如何得到分數1:每個輸入的數字 – jmike

回答

1

有很多數據結構的這將允許您按順序存儲數據。我個人會爲此推薦List<int>

您可以簡單地添加一些東西把它稱爲:

var list = new List<int>(); 
list.Add(37); 
list.Add(95); 

而且你可以用一個迭代器(foreach(var score in list){...})閱讀或取得個人數字出來(var firstScore = list[0])。該文件將告訴你更多關於你可以用List<T>做什麼。

0

您可以在C#(MSDN Collections)中查找可用的集合。

在你的情況,你真的不關心順序,你可以使用List<int>。否則,如果您想保留訂單,您可以使用Stack<int>Queue<int>。如果你想保持學生姓名+集合分數,你可以使用一個Dictionary<string,int>

+0

'List '也會保持順序。它們與堆棧/隊列之間的區別在於,您可以訪問列表中的任何項目,但堆棧和隊列僅以特定順序返回內容。它們都按照您定義的順序存儲內容。 – Chris

2

聲明一個變量InputGrade方法計算像private static int counter = 0;
投入,把像下面

Console.WriteLine("{0}\n{1}", 
       "Enter the integer grades in the range 0-100", 
       "Type <Ctrl> z and press Enter to terminate input:"); 
counter++; 
System.Console.Write("score " + counter + ":"); 
input = Console.ReadLine(); //read user input 

和內部while (input != null)把像下面

IncrementLetterGradeCounter(grade); 
counter++; 
System.Console.Write("score " + counter + ":"); 
input = Console.ReadLine(); 

所以,輸出會像 enter image description here

下面是完整的代碼

public class Program 
    { 
     private int total; // sum of grades 
     private int gradeCounter; //number of grades entered 
     private int aCount; // Count of A grades 
     private int bCount; // Count of B grades 
     private int cCount; // Count of C grades 
     private int dCount; // Count of D grades 
     private int fCount; // Count of F grades 
     private string v; 
     private static int counter = 0; 


     public string CourseName { get; set; } 

     public Program(string name) 
     { 
      CourseName = name; 
     } 

     public void DisplayMessage() 
     { 
      Console.WriteLine("Welcome to the grade book for \n{0}!\n", 
       CourseName); 
     } 

     public void InputGrade() 
     { 
      int grade; 
      string input; 

      Console.WriteLine("{0}\n{1}", 
       "Enter the integer grades in the range 0-100", 
       "Type <Ctrl> z and press Enter to terminate input:"); 
      counter++; 
      System.Console.Write("score " + counter + ":"); 
      input = Console.ReadLine(); //read user input 

      while (input != null) 
      { 
       grade = Convert.ToInt32(input); //read grade off user input 
       total += grade;// add grade to total 
       gradeCounter++; // increment number of grades 

       IncrementLetterGradeCounter(grade); 
       counter++; 
       System.Console.Write("score " + counter + ":"); 
       input = Console.ReadLine(); 
      } 
     } 
     private void IncrementLetterGradeCounter(int grade) 
     { 
      switch (grade/10) 
      { 
       case 9: //grade was in the 90s 
       case 10: 
        ++aCount; 
        break; 
       case 8: 
        ++bCount; 
        break; 
       case7: 
        ++cCount; 
       case6: 
        ++dCount; 
        break; 
       default: 
        ++fCount; 
        break; 

      } 
     } 
     public void DisplayGradeReport() 
     { 
      Console.WriteLine("\nGrade Report"); 

      if (gradeCounter != 0) 
      { 
       double average = (double)total/gradeCounter; 

       Console.WriteLine("Total of the {0} grades entered is {1}", 
        gradeCounter, total); 
       Console.WriteLine("class average is {0:F}", average); 
       Console.WriteLine("{0}A: {1}\nB: {2}\nC: {3}\nD: {4}\nF: {5} ", 
        "Number of students who received each grade: \n", 
        aCount, 
        bCount, 
        cCount, 
        dCount, 
        fCount); 
      } 
      else 
       Console.WriteLine("No grades were entered"); 
     } 
     static void Main(string[] args) 
     { 
      Program mygradebook = new Program(
       "CS101 introduction to C3 programming"); 
      mygradebook.DisplayMessage(); 
      mygradebook.InputGrade(); 
      mygradebook.DisplayGradeReport(); 
      Console.ReadKey(); 
     } 
    } 
相關問題