简体   繁体   中英

How to Display an Array in C#?

so I'm new to coding and I've decided to learn C# with the whole Covid-19 going on, and I've run into a small problem if anybody can assist me.

I'm just writing a basic C# program to allow the user to input 5 numbers into an array and then display the array, but for some reason, I only display the number 5, not the whole array.

Please find my code: ( if anyone can make it easier for me please help me lol (: )

  int[] numbers = new int[5];
  int num = 0;
  int i = 0;

  Console.WriteLine("This porgram allows the user to input 5 numbers into an array");
  Console.Write("--------------------------------------------------------------");
  Console.WriteLine("\n");

  for ( i = 0; i < numbers.Length; i ++)
  {
    Console.WriteLine("Please input number");
    num = Convert.ToInt32(Console.ReadLine());
  }

  for (i = 0; i < numbers.Length; i++)
  {
     Console.ReadLine(); 
     Console.WriteLine("Your array is: " , numbers );
  }

  Console.WriteLine();

// any help will be appreciated!

Two problems:

1) You haven't put the number coming in. After

num = Convert.ToInt32(Console.ReadLine());

put

 numbers[i] = num;

Though in fact num is superfluous, so you could just have

 numbers[i]= Convert.ToInt32(Console.ReadLine());

2) In the second loop you need to display the specific array element:

   Console.WriteLine("Your array item is: " , numbers[i] );

Also, not sure what the ReadLine() in the second loop is for - just means the users has to hit return to see each number.

It's worth mentioning a few other issues in the code:

  1. Variables should be declared as close as possible to where they are used. this i should be declared separately for each for loop - for(int i = 0; ... and num should be declared inside the loop (though as mentioned, it's redundant).

  2. Be clear on the difference between Console.Write() and Console.WriteLine() . WriteLine() simply adds a \\n to whatever is displayed. Thus it would be clearer (for the same output to have:

    Console.WriteLine("--------------------------------------------------------------"); Console.WriteLine();

here is code: it is the basic syntax of displaying an array.

  public class Work  
  {  
   public static void Main()  
     {  
       int[] arr = new int[5]; 
       int i;  
       Console.Write("\nRead & Print elements of an array:\n");
       Console.Write("-----------------------------------------\n");    

       Console.Write("Input 5 elements in the array :\n");  
       for(i=0; i<5; i++)  
        {  
          Console.Write("element - {0} : ",i);
          arr[i] = Convert.ToInt32(Console.ReadLine());         
        }  

         Console.Write("\nElements in array are: ");  
         for(i=0; i<5; i++)  
         {  
           Console.Write("{0}  ", arr[i]);  
         } 
           Console.Write("\n"); 
      }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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