【问题标题】:Changing value in a array and display更改数组中的值并显示
【发布时间】:2014-03-06 16:52:51
【问题描述】:

您好,我编写了一个代码,该代码应该显示每个人的姓名,然后能够选择我选择的任何人,然后显示他们的评分并让我能够更改它。我得到它来显示他们的成绩,但我不知道如何只改变那个人的成绩值,而是改变每个人的成绩并显示它我该如何改变它?所以它只显示那个人的变化?并添加我想要的任何价值?以及如何在人名旁边显示更改的值?如果你能帮助我,我非常感谢。

    static void Main(string[] args)
    {
        int selection;
        do
        {
            String[] A = { "Tim", "Jhon", "Sam", "Derp"};
            int[] B = { 90, 89, 60, 40 };
            DisplayMenu();
            Console.Write("Enter you selection: ");
            int.TryParse(Console.ReadLine(),out selection);
            switch (selection)
            {
                case 1:
                    Console.WriteLine(
            "{0}  ", B[0]);
                    AddToGrade(B);
                    break;
                case 2:
                    Console.WriteLine(
            "{0}  ", B[1]);
                    AddToGrade(B);
                    break;
                case 3:
                    Console.WriteLine(
            "{0}  ", B[2]);
                    AddToGrade(B);
                    break;
                default:
                    Console.WriteLine(
            "{0}  ", B[3]);
                    AddToGrade(B);
                    break;
            }
            Console.ReadKey();
        } while (selection != 4);

    }
    static void DisplayMenu()
    {
        Console.Clear();
        Console.WriteLine("Select a student 1-4");
        Console.WriteLine("1. Time");
        Console.WriteLine("2. Jhon");
        Console.WriteLine("3. Sam");
        Console.WriteLine("4. Derp");
    }
    static void AddToGrade(int[] array)
    {

        Console.WriteLine("Input New Value: ");
        int newValue = Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i < array.Length; i++)
        {
            array[i] += newValue;
            Console.Write("New Grade: {0}", array[i]);
        }
    }

【问题讨论】:

  • 由于 C# 是一种面向对象的语言,您应该考虑创建一个 Person 或 Student 类。
  • 我完全同意 Mert,虽然我写了一个答案,但你应该在修复代码时考虑这个评论。
  • @Ofiris 感谢您的意见和帮助。

标签: c# arrays methods indexing


【解决方案1】:

AddToGrade 应该接受index 代表要更改的等级。

static void AddToGrade(int[] array ,int index)
{
     Console.WriteLine("Input New Value: ");
     int newValue = Convert.ToInt32(Console.ReadLine());
     array[index] += newValue;
     Console.Write("New Grade: {0}", array[index]);
}

在你切换之后(在Console.ReadKey(); 之前)只需调用这个:

AddToGrade(B, selection - 1);

此外,你应该使用更多的函数来使你的代码健壮和优雅, 例如(该示例还允许您将格式保存在一个位置)

static void DisplayNameGrade(int index, String[] A, int[] B)
{
    Console.WriteLine("Name: {0}, Grade: {1}", A[index], B[index])
}

一般来说,你应该学习Object Oriented Programming的基本概念,并用类、成员和函数来设计你的代码。

【讨论】:

  • 嗯,我在数组 [i] 处收到一个蓝色错误代码,它在当前上下文中不存在。
  • 应该是array[index]
  • 我应该将 DisplayNameGrade 放在我的案例中还是添加其他内容?抱歉,我对 C# 还是很陌生。
  • 你可以让 A 和 B 成为全局变量,或者将它们作为函数参数传递,我编辑了我的帖子。
  • 是的,我只是这样做了,因为我看到它不见了。
猜你喜欢
  • 1970-01-01
  • 2016-05-25
  • 2018-04-27
  • 2021-06-05
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
相关资源
最近更新 更多