-1

I am a Beginner in C# and I was wondering what I got wrong with this code.

  1. I want the user to input the amount of numbers
  2. Then an array with that amount gets created
  3. Then finally I want to display all the numbers in the array.

The Code:

using System;
using System.Threading;
using System.Collections.Generic;

namespace Console_Project_alpha
{
    
class Program 
{
    
    static void Main(string[] args)
    {
            Console.Write("Enter the amount of numbers: ");
            int amount = Convert.ToInt32(Console.ReadLine());
            
            int[] numbers = new int[amount];
            string nth = "";

            
            
            for( int i = 1; i <= amount ; i++)
            {
                        if(i == 1)
                        {
                            nth = i + "st";    
                        }
                        else if( i == 2)
                        {
                           nth = i + "nd";
                        }
                        else if( i == 3)
                        {
                            nth = i + "rd";   
                        }else{
                            nth = i + "th";
                        }
                
                Console.Write("\nEnter " + nth + " Number:");
                int num = Convert.ToInt32(Console.ReadLine());
               
               for(int j = 0; j <= numbers.Length; j++)
                {
                    numbers[j] = num;
                    
                }
                
            }
            System.Console.WriteLine(numbers);

            

    }

   }
}

Any help is highly appreciated. Thanks in advance

eglease
  • 2,445
  • 11
  • 18
  • 28
D_W
  • 39
  • 5

1 Answers1

1
  1. In your code you all time overwriting the same value to all index in array.
  2. If you want to display values from array in console just iterate after array

Properly worked example based on your code:

class Program
{

    static void Main(string[] args)
    {
        Console.Write("Enter the amount of numbers: ");
        int amount = Convert.ToInt32(Console.ReadLine());

        int[] numbers = new int[amount];
        string nth = "";
        int index = 0;

        for (int i = 1; i <= amount; i++)
        {
            if (i == 1)
            {
                nth = i + "st";
            }
            else if (i == 2)
            {
                nth = i + "nd";
            }
            else if (i == 3)
            {
                nth = i + "rd";
            }
            else
            {
                nth = i + "th";
            }

            Console.Write("\nEnter " + nth + " Number:");
            int num = Convert.ToInt32(Console.ReadLine());

            numbers[index] = num;
            index++;
        }

        for (int i = 0; i <= numbers.Length - 1; i++)
        {
            Console.WriteLine(numbers[i]);
        }

        Console.ReadLine();
    }

}
Hararr 1
  • 26
  • 2
  • now I come back and look at it.. this was an easy question I wonder, how in the hell did I ask this.. xD Thanks anyways ;) – D_W Dec 20 '21 at 21:24