+ 1
Why the array canceled it's index value (input function) when it calls to print in output function?
public class system{ private int[] arr = new int[4]; public int[] Arr { get { return arr; } set { arr = value; } } public void InPut() { foreach (int i in arr) { Arr[i] = int.Parse(Console.ReadLine()); } } public void OutPut() { for (int i = 0; i < Arr.Length; i++) { Console.WriteLine(Arr [i]); } } class program { system s1=new system(); s1.InPut(); s1.OutPut(); }
4 Réponses
0
if you use foreach you get the single element of the group. So you can say
i = int.Parse(Console.ReadLine ()); instead of arr[i]
Notice the use of i is confusing here, you can use words for your variables like
foreach(int number in arr) or
foreach(int arrayelement in arr) or
foreach (int balloon in arr)
balloon is just a randomly choosen name.
Make it more descriptive.
0
i cannot assign to i because it is a foreach iteration variable
0
Oeps, That is true.
i in foreach is readonly.
If you want to change the value of the element of a array, you need a for loop.
https://www.dotnetperls.com/foreach
0
Did write some code to see show that this was the case.
Please do not name any of you classes "system". Yes C# is case sensitive but System is the most important class of all the classes. You do not want to confuse.
Note that you can leave out the Arr property since you do everything in the class and only do call the methods to show the data.
https://code.sololearn.com/cQ7zC4fz06Bs/#cs