如何将值返回到数组

Posted

技术标签:

【中文标题】如何将值返回到数组【英文标题】:How to return a value to an array 【发布时间】:2019-03-18 15:13:18 【问题描述】:

我正在做一个简单的民意调查/调查来检查用户可以去哪里工作。我回答是或否。我做了一个积分计数器,所以如果他回答是,它可以检查用户信息然后加一分。我想创建一个显示问题并检查用户输入的函数,而不是为每个问题编写相同的 do while 循环。我制作了一个用于收集“用户点”的数组。但是,问题在于,由于程序正在跳转到循环并添加 +1 点,所以它不能向这个“点数组”返回值。该值位于内存中的其他位置,但不在数组中。这导致无法正常工作的摘要。它只是到处显示每个可能的工作的 0 分。我做错了什么或者我可以做些什么来使它正常工作?

这是我的代码(我可能通过复制/粘贴弄乱了大括号的格式):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Survey

    class Program
    
        static void Main(string[] args)
        
        //here's my Question Bank

        ArrayList QuestionList = new ArrayList();
        QuestionList.Add("1. Question");
        QuestionList.Add("2. ...");
        QuestionList.Add("3. ...");
        QuestionList.Add("4. ...");
        QuestionList.Add("5. ...");
        QuestionList.Add("6. ...");
        QuestionList.Add("7. ...");
        QuestionList.Add("8. ...");
        QuestionList.Add("9. ...");
        QuestionList.Add("10. ...");

        //here's my work list.
        ArrayList WorkList = new ArrayList();
        WorkList.Add("IT");
        WorkList.Add("Architect");
        WorkList.Add("Politician");
        WorkList.Add("Driver");
        WorkList.Add("Designer");

        //here's an array, where I want to hold "points". The higher points the more probably user will get suggestion where to work.
        int[] Work;
        Work = new int[5]  0, 0, 0, 0, 0 ;

        Console.WriteLine("Hi. Say 'y' if you agree or 'n' if not.");

        displayQuestion(QuestionList[0], Work[0]);
        displayQuestion(QuestionList[1], Work[1]);
        displayQuestion(QuestionList[2], Work[2]);
        displayQuestion(QuestionList[3], Work[3]);
        displayQuestion(QuestionList[4], Work[4]);
        displayQuestion(QuestionList[5], Work[4]);
        displayQuestion(QuestionList[6], Work[1]);
        displayQuestion(QuestionList[7], Work[2]);
        displayQuestion(QuestionList[8], Work[0]);
        displayQuestion(QuestionList[9], Work[3]);

        // here's calculating maximum points 
        int max;
        max = Work[0];
        for (int i=0; i<5; i++)
        
            if (Work[i] > max)
                max = Work[i];
        

        for (int i = 0; i < 5; i++)
        
            if(Work[i]==max)
            Console.WriteLine("You can work as: " + WorkList[i]);
        

        //Summary
        Console.WriteLine("Points as: " + WorkList[0] + " = " + Work[0]);
        Console.WriteLine("Points as: " + WorkList[1] + " = " + Work[1]);
        Console.WriteLine("Points as: " + WorkList[2] + " = " + Work[2]);
        Console.WriteLine("Points as: " + WorkList[3] + " = " + Work[3]);
        Console.WriteLine("Points as: " + WorkList[4] + " = " + Work[4]);

        Console.ReadLine();

    

    //here's the PROBLEM (I think)
    public static int displayQuestion(object whichQuestion, int WorkPoints)
    
        string answer;

        do
        
            Console.WriteLine(whichQuestion);
            answer = Console.ReadLine();
            if (answer == "y")
            
                WorkPoints++;
            
         while (answer != "y" && answer != "y");
        return WorkPoints;
        
    

【问题讨论】:

你能分享一个minimal reproducible example吗? 我会尝试编辑代码 尝试将您的函数更改为以下int displayQuestion(object whichQuestion, ref int WorkPoints) 并像displayQuestion(QuestionList[0],ref Work[0]) 一样调用它 谢谢@SamvelPetrosov 现在终于可以使用了! @MarcinChmurowski 你做了哪些改变效果很好??? 【参考方案1】:

实际上,您将新分数分配给 displayQuestion 方法的返回值,而不是使用它。

public static int displayQuestion(object whichQuestion, int WorkPoints)

所以一种可能的方法是使用the ref 关键字作为Samvel 所说的或将方法的返回值分配给work[i]

Work[0] = displayQuestion(QuestionList[0], Work[0]);

【讨论】:

【参考方案2】:

把函数改成如下:

public static int displayQuestion(object whichQuestion)

    string answer;
    int WorkPoints = 0;
    do
    
        Console.WriteLine(whichQuestion);
        answer = Console.ReadLine();
        if (answer == "y")
        
            WorkPoints++;
        
     while (answer != "y" && answer != "n");
    return WorkPoints;
    

然后这样使用:

Work[0] += displayQuestion(QuestionList[0]);

【讨论】:

以上是关于如何将值返回到数组的主要内容,如果未能解决你的问题,请参考以下文章

在 CoffeeScript 中,如何将值附加到数组?

如何将值附加到命令行参数数组?

如何将值添加到数组 - Angular2

如何将值的数组参数输入到 Firebird 存储过程?

如何将值添加到从颤振到 Firestore 的数组中

如何将值添加到其他类的数组列表中?