我如何为每个用户输入编号? C#

时间:2017-03-07 09:32:38

标签: c#

刚开始学习C#,我的问题是我如何按照这样的顺序记录用户输入:score 1:

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();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

声明一个变量来计算private static int counter = 0;之类的输入 在InputGrade方法中,如下所示

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();
        }
    }

答案 1 :(得分: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>可以执行的操作的更多信息。

答案 2 :(得分:0)

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

在您的情况下,您并不真正关心订单,您可以使用List<int>。否则,如果您想保留订单,可以使用Stack<int>Queue<int>。如果您想保留学生姓名+分数的集合,可以使用Dictionary<string,int>