Visual Basic中的C# - 如何从并行数组中添加整数?

时间:2015-11-04 01:47:58

标签: c# arrays visual-studio

所以我设置了数组,并将它们分配给设计主窗口视图中的值。它的设置就像一个测验,每个问题(if语句)都是一个具有不同点值的问题。我无法弄清楚如何在最后添加每个问题的所有点。

namespace ParallelArrRbnQuiz
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string[] arrUserAnswer = new string[10];
        int[] arrPoints = new int[10];
        string[] arrCorrectAnswer = new string[10];
        int intScore = 0;


        public MainWindow()
        {
            InitializeComponent();
            arrCorrectAnswer[0] = "Loop";
            arrCorrectAnswer[1] = "Conditional";
            arrCorrectAnswer[2] = "Variable";
            arrCorrectAnswer[3] = "Java";
            arrCorrectAnswer[4] = "Decision";
            arrCorrectAnswer[5] = "XAML";
            arrCorrectAnswer[6] = "Compiler";
            arrCorrectAnswer[7] = "Array";
            arrCorrectAnswer[8] = "Syntax Error";
            arrCorrectAnswer[9] = "Camel Casing";

            arrPoints[0] = 5;
            arrPoints[1] = 10;
            arrPoints[2] = 15;
            arrPoints[3] = 5;
            arrPoints[4] = 10;
            arrPoints[5] = 20;
            arrPoints[6] = 5;
            arrPoints[7] = 5;
            arrPoints[8] = 5;
            arrPoints[9] = 20;

            arrUserAnswer[0] = "Loop";
            arrUserAnswer[1] = "Conditional";
            arrUserAnswer[2] = "Variable";
            arrUserAnswer[3] = "Java";
            arrUserAnswer[4] = "Decision";
            arrUserAnswer[5] = "XAML";
            arrUserAnswer[6] = "Compiler";
            arrUserAnswer[7] = "Array";
            arrUserAnswer[8] = "Syntax Error";
            arrUserAnswer[9] = "Camel Casing";
        }
        private void Submit_Answers(object sender, RoutedEventArgs e)
        {
            if(arrCorrectAnswer[0] == arrUserAnswer[0])
            {
                intScore += arrPoints[0];
            }
            if(arrCorrectAnswer[1] == arrUserAnswer[1])
            {
                intScore += arrPoints[1];
            }
            if(arrCorrectAnswer[2] == arrUserAnswer[2])
            {
                intScore += arrPoints[2];
            }
            if(arrCorrectAnswer[3] == arrUserAnswer[3])
            {
                intScore += arrPoints[3];
            }
            if(arrCorrectAnswer[4] == arrUserAnswer[4])
            {
                intScore += arrPoints[4];
            }
            if(arrCorrectAnswer[5] == arrUserAnswer[5])
            {
                intScore += arrPoints[5];
            }
            if(arrCorrectAnswer[6] == arrUserAnswer[6])
            {
                intScore += arrPoints[6];
            }
            if(arrCorrectAnswer[7] == arrUserAnswer[7])
            {
                intScore += arrPoints[7];
            }
            if(arrCorrectAnswer[8] == arrUserAnswer[8])
            {
                intScore += arrPoints[8];

            if(arrCorrectAnswer[9] == arrUserAnswer[9])
            {
                intScore += arrPoints[9];
            }
                txtScore.Text = intScore.ToString();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

这是我从您的代码

中理解的
for(int i=0;i<10;i++)
{
    if(arrCorrectAnswer[i] == arrUserAnswer[i])
    {
        arrPoints[i] += arrPoints[i];
    }
}

然后

int TotalPoints = 0;
for(int i=0;i<10;i++)
{
    TotalPoints += arrPoints[i];
}

但我相信它应该是

int TotalPoints = 0;
for(int i=0;i<10;i++)
{
    if(arrCorrectAnswer[i] == arrUserAnswer[i])
    {
        TotalPoints += arrPoints[i];
    }
}

之后,您可以展示TotalPoints

答案 1 :(得分:0)

使用计数器遍历数组中的每个元素。您可以创建一个类似这样的方法:

var counter = 0;
while (counter <= arrPoints.Length(){
    counter += arrPoints[counter]
}
return counter;

答案 2 :(得分:0)

您可以使用Sum Linq Extension方法获取阵列的总数 (注意:FrameWork 3.5及更高版本

static void Main(string[] args)
{
    int[] arrPoints = new int[] { 5, 10, 15, 5, 10, 20, 5, 5, 5, 20 } ;
    int value = arrPoints.Sum();
    Console.WriteLine(value);
    Console.ReadLine();

}