Storing input integer numbers in an array

I am trying to ask the user to enter 10 numbers. After getting the numbers, I save them in an array, and then print the array. To complete the task, I came up with the following code, but it does not print the array.

Also feel like I may have popped too much code for a simple task. Note that I am very new to C #, so I am not familiar with advanced materials, or perhaps even with most basic materials. Even "convert.toInt32", I accepted from reading and have not yet taught in the classroom.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            int d;
            int e;
            int f;
            int g;
            int h;
            int i;
            int j; 

            Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            d = Convert.ToInt32(Console.ReadLine());
            e = Convert.ToInt32(Console.ReadLine());
            f = Convert.ToInt32(Console.ReadLine());
            g = Convert.ToInt32(Console.ReadLine());
            h = Convert.ToInt32(Console.ReadLine());
            i = Convert.ToInt32(Console.ReadLine());
            j = Convert.ToInt32(Console.ReadLine());

            int[] newArray = {a,b,c,d,e,f,g,h,i,j};

            Console.WriteLine(newArray);

            Console.ReadLine();
        }
    }
}
+4
source share
4 answers

ToString ( , Console.WriteLine ) , . object .

( , ).

..

foreach(var item in array)
    Console.WriteLine(item)

Console.WriteLine(string.Join("\n", array));
+3

for.

int[] newArray = new int[10];
for (int i = 0; i < newArray.Length; i++)
{
    newArray[i] = Convert.ToInt32(Console.ReadLine());
}

:

for (int i = 0; i < newArray.Length; i++)
{
    Console.WriteLine(newArray[i]);
}
+7
    static void Main(string[] args)
    {
        int[] rollno = new int[10];
        Console.WriteLine("Enter the 10 numbers");
        for (int s = 0; s < 9; s++)
        {
            rollno[s] = Convert.ToInt32(Console.ReadLine());
            rollno[s] +=  110;

        }
        for (int j = 0; j < 9; j++)
        {
            Console.WriteLine("The sum of first 10 numbers is : {0}", rollno[j]);
        }
        Console.ReadLine();
    }
}

}

+1
source

You can greatly simplify much of the following:

static void Main(string[] args)
{
    int newArray = new int[10];

    Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
    for (int i = 0; i < 10; i++) {
        newArray[i] = Convert.ToInt32(Console.ReadLine());
    }

    Console.WriteLine("The values you've entered are:");
    Console.WriteLine(String.Join(", ", newArray));

    Console.ReadLine();
}
0
source

All Articles