简体   繁体   English

C#中的元素输出错误

[英]Wrong element output in C#

I apologize in advance, but I'm fairly new to C# in this context (I've used it in Unity, but never on its own). 我事先表示歉意,但是在这种情况下,我对C#还是陌生的(我在Unity中使用过它,但从来没有单独使用过)。

I'm trying to create a string array, and within a loop, have another array with, let's say, a rating system (int elements). 我正在尝试创建一个字符串数组,并在一个循环内创建另一个带有评级系统(int元素)的数组。 Unfortunately, my problem seems on a level of its own. 不幸的是,我的问题似乎只是一个层面。

This is the code: 这是代码:

using System;

public class Test
{
  public static void Main()
  {
    string [] arr1 = new string[] {"a","b","c","d","e"};
    int a = 0;
    foreach (var element in arr1)
    {          
        int [] myRate = new int [5] {1,2,3,4,5};
        int [] rating = myRate;
        a = rating[0];
        Console.WriteLine (element +": " + (rating[a]));
    } a++;
  }
}

My problem is the output is supposed to be 1 (for all elements because I'm still working on the incrementation), but this is the output: 我的问题是输出应该为1(对于所有元素,因为我仍在进行增量操作),但这是输出:

a: 2
b: 2
c: 2
d: 2
e: 2

I tried to reduce the code as much as possible to pinpoint the problem (that's why it's kind of minimalistic), but I still cannot figure it out. 我试图尽可能地减少代码以查明问题(这就是为什么它很简单),但是我仍然无法弄清楚。 When I change the zero to 1 the output is 2, and so on. 当我将零更改为1时,输出为2,依此类推。 If I change it to 4 it says its out of bound. 如果我将其更改为4,则表示超出范围。 Your help is much appreciated, and thank you in advance. 非常感谢您的帮助,并在此先感谢您。

Inside your loop, when do this: 在循环中,何时执行此操作:

a = rating[0];

then a becomes 1, because it's the first item in your int array. 然后a变为1,因为它是int数组中的第一项。 Then you write this to console: 然后将其写入控制台:

rating[a]

...which results to this (since a is 1): ...导致的结果(因为a为1):

rating[1]

...and the second item in your array is 2, that's why you get this output. ...而数组中的第二项是2,这就是为什么要获得此输出的原因。


Two ways of outputting 1: 两种输出方式1:

foreach (var element in arr1)
{          
    int [] myRate = new int [5] {1,2,3,4,5};
    int [] rating = myRate;
    a = 0;
    Console.WriteLine (element +": " + (rating[a]));
} a++;

and

foreach (var element in arr1)
{          
    int [] myRate = new int [5] {1,2,3,4,5};
    int [] rating = myRate;
    Console.WriteLine (element +": " + (rating[0]));
} a++;

As a side note, you may consider creating the int[] outside your loop, since you are every iteration creating the same array, unless you actually intend to create arrays with different values every new iteration. 附带说明一下,您可以考虑在循环外创建int[] ,因为每次迭代都创建相同的数组,除非您实际上打算在每次新迭代中创建具有不同值的数组。

Because "a" contains number "1". 因为“ a”包含数字“ 1”。

And number 1 on index of rating array is 2. 评级数组索引上的数字1是2。

If you want to print number 1, you should start at index 0. Which is: 如果要打印数字1,则应从索引0开始。即:

Console.WriteLine (element +": " + (rating[0]));

If you want 1 in answer then write: 如果您希望答案为1,请输入:

Console.WriteLine (element +": " + a);

or 要么

Console.WriteLine (element +": " + rating[0]);

because a=rating[0]; 因为a=rating[0]; ie first element of array whose value is 1. 即数组的第一个元素,其值为1。

you are using: 您正在使用:

Console.WriteLine (element +": " + (rating[a]));

ie rating[1] because value of a is 1 . rating[1]因为a is 1

so rating[1] is second element of array whose value is 2 所以rating[1]是数组的第二个元素,其值为2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM