gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:26 26 4
gpt4 key购买 nike

刚开始学习 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();
}
}
}

最佳答案

有很多数据结构可以让您按顺序存储数据。我个人推荐 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# - 我如何为每个用户输入编号? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com