簡體   English   中英

我如何為每個用戶輸入編號? C#

[英]how can i number each user input? 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();
        }
    }
}

聲明一個變量來計算像private static int counter = 0;這樣的輸入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();

所以,輸出就像 在此輸入圖像描述

這是完整的代碼

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

有許多數據結構可以讓您按順序存儲數據。 我個人推薦一個List<int>

您可以像下面這樣添加內容:

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

您可以使用迭代器( foreach(var score in list){...} )讀取它,或者獲取單個數字( var firstScore = list[0] )。 該文檔將告訴您有關List<T>

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM