har bir foydalanuvchi ma'lumotlarini qanday qilib kiritish mumkin? C #

Just started learning C#, my question is how do i keep record of the user input in order like this: score 1:

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

Mening kodamda raqamni kiritishim mumkin, ammo bu maqsadga qanday erishishim mumkinligini tartibga solish kabi ko'rinmaydi Mening kirishim:

98
76
65
78
56

kod:

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  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();
        }
    }
}
1
Siz xohlagan narsangiz haqida aniq ma'lumot yo'q. Ayniqsa, ushbu maktab topshirig'ida bir oz tadqiqot va harakat qiling.
qo'shib qo'ydi muallif EpicKip, manba
Siz xohlagan narsangiz haqida aniq ma'lumot yo'q. Ayniqsa, ushbu maktab topshirig'ida bir oz tadqiqot va harakat qiling.
qo'shib qo'ydi muallif EpicKip, manba
Siz xohlagan narsangiz haqida aniq ma'lumot yo'q. Ayniqsa, ushbu maktab topshirig'ida bir oz tadqiqot va harakat qiling.
qo'shib qo'ydi muallif EpicKip, manba
Siz kiritgan tartib bilan ishlashni istagan narsangiz bormi? bu faqat bitta indeksmi yoki u biror narsa uchun ishlatiladimi?
qo'shib qo'ydi muallif Dave, manba
Siz kiritgan tartib bilan ishlashni istagan narsangiz bormi? bu faqat bitta indeksmi yoki u biror narsa uchun ishlatiladimi?
qo'shib qo'ydi muallif Dave, manba
Siz kiritgan tartib bilan ishlashni istagan narsangiz bormi? bu faqat bitta indeksmi yoki u biror narsa uchun ishlatiladimi?
qo'shib qo'ydi muallif Dave, manba
Men birinchi pog'onani 98 ga, ikkinchi daraja 76 bo'lsa, 98:76 ni o'rniga 1:98, 2:76 ni olasizmi? Keyinroq siz "2-son" nima deb so'roq qila olasiz? Buni faqat ma'lumotni biror joyda saqlash yoki ro'yxat yordamida saqlashingiz mumkin.
qo'shib qo'ydi muallif Dave, manba
@dave Men faqat bilib olishni bilmoqchi edim: 1-raqam: har bir kirish uchun raqamli
qo'shib qo'ydi muallif jmike, manba
@jmike, Chris tomonidan berilgan MSDN linkiga qarashadi. ro'yxatida o'zingizni Sum() , Average() va Count() .
qo'shib qo'ydi muallif Mong Zhu, manba
@jmike, Chris tomonidan berilgan MSDN linkiga qarashadi. ro'yxatida o'zingizni Sum() , Average() va Count() .
qo'shib qo'ydi muallif Mong Zhu, manba

6 javoblar

declare one variable to count inputs like private static int counter = 0;
in InputGrade method, put like below

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

va while (input! = null) ichida quyida keltirilgan

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

so, output will be like enter image description here

Bu erda to'liq kod

    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  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();
        }
    }
2
qo'shib qo'ydi

There are a lot of data structures that will allow you to store data in order. I'd personally recommend a List for this.

Bunga oddiy narsalarni qo'shishingiz mumkin:

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

And you can either read it with an iterator (foreach(var score in list){...}) or get individual numbers out (var firstScore = list[0]). The documentation will tell you more about what you can do with a List.

1
qo'shib qo'ydi

There are a lot of data structures that will allow you to store data in order. I'd personally recommend a List for this.

Bunga oddiy narsalarni qo'shishingiz mumkin:

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

And you can either read it with an iterator (foreach(var score in list){...}) or get individual numbers out (var firstScore = list[0]). The documentation will tell you more about what you can do with a List.

1
qo'shib qo'ydi

C # -da mavjud bo'lgan to'plamlarni topishingiz mumkin ( MSDN Collections ).

In your case, you don't really care about order, you can use List. Otherwise if you want to keep an order you can use Stack or Queue. And if you want to keep a collection of Student Name + Score you can use a Dictionary

0
qo'shib qo'ydi
Ro'yxat narsalarni tartibda saqlaydi. Ularning orasidagi farqlar/navbatlar o'rtasidagi farq, ro'yxatdagi biron-bir narsaga kirishingiz mumkin, biroq stack va quyruq faqat mazmunini juda aniq tartibda qaytarib beradi. Ularning barchasi tarkibini siz aniqlagan tartibda saqlaydi.
qo'shib qo'ydi muallif Chris, manba

C # -da mavjud bo'lgan to'plamlarni topishingiz mumkin ( MSDN Collections ).

In your case, you don't really care about order, you can use List. Otherwise if you want to keep an order you can use Stack or Queue. And if you want to keep a collection of Student Name + Score you can use a Dictionary

0
qo'shib qo'ydi
Ro'yxat narsalarni tartibda saqlaydi. Ularning orasidagi farqlar/navbatlar o'rtasidagi farq, ro'yxatdagi biron-bir narsaga kirishingiz mumkin, biroq stack va quyruq faqat mazmunini juda aniq tartibda qaytarib beradi. Ularning barchasi tarkibini siz aniqlagan tartibda saqlaydi.
qo'shib qo'ydi muallif Chris, manba

C # -da mavjud bo'lgan to'plamlarni topishingiz mumkin ( MSDN Collections ).

In your case, you don't really care about order, you can use List. Otherwise if you want to keep an order you can use Stack or Queue. And if you want to keep a collection of Student Name + Score you can use a Dictionary

0
qo'shib qo'ydi
Ro'yxat narsalarni tartibda saqlaydi. Ularning orasidagi farqlar/navbatlar o'rtasidagi farq, ro'yxatdagi biron-bir narsaga kirishingiz mumkin, biroq stack va quyruq faqat mazmunini juda aniq tartibda qaytarib beradi. Ularning barchasi tarkibini siz aniqlagan tartibda saqlaydi.
qo'shib qo'ydi muallif Chris, manba