简体   繁体   中英

C# array help (i'm a beginner) :)

Currently this code asks the user ONCE to enter their name and score which is then outputted on screen:

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

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        string ST_AR_NAMES;
        int IN_AR_Scores;
        Console.WriteLine("Please enter your name ");
        ST_AR_NAMES=Console.ReadLine();

        Console.WriteLine("Please enter your score");
        IN_AR_Scores = Convert.ToInt32(Console.ReadLine());


        for (int i = 0; i < ST_AR_NAMES.Length; i++)
        {
            Console.WriteLine("Name: {0} - score: {1}", ST_AR_NAMES, IN_AR_Scores);
            break;

        }
        Console.ReadLine();
    }
  }
}

How do i make this code ask the user to input/output their name and score...10 times?

You need to take your for loop and include the rest of your code as such:

    for (int i = 0; i < 10; i++)
    {
    string ST_AR_NAMES;
    int IN_AR_Scores;
    Console.WriteLine("Please enter your name ");
    ST_AR_NAMES=Console.ReadLine();

    Console.WriteLine("Please enter your score");
    IN_AR_Scores = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Name: {0} - score: {1}", ST_AR_NAMES, IN_AR_Scores);
        break;

    }

I've configured it to loop through 10 times. Your initial post is a bit vague. Does this help?

If you would like to enter the number of users, and track an id for each. Added comments

class Program
{
    static void Main(string[] args)
    {     
        // A List to store our users
        List<EnteredUser> _names = new List<EnteredUser>();
        // Ask how many users to get
        Console.WriteLine("How many users would you like to enter? ");
        // Store number of loops
        int _numberOfLoops = Convert.ToInt32(Console.ReadLine());
        // Loop however many times we said
        for (int i = 0; i < _numberOfLoops; i++)
        {
            // Create a new user object with the loop index as id
            var newUser = new EnteredUser() { id = i };
            // Get users name
            Console.WriteLine("Please enter your name ");
            newUser.Name = Console.ReadLine();
            // Get users score
            Console.WriteLine("Please enter your score");
            newUser.score = Convert.ToInt32(Console.ReadLine());
            // Add user to our list
            _names.Add(newUser);
        }

        // For each user we collected
        foreach (var item in _names)
        {
            // Write their name without break so we get a list
            Console.WriteLine("Name: {0} - score: {1}", item.Name, item.score);
        }

        // Stop
        Console.ReadLine();
    }
}

/// <summary>
/// Class to store user detaisl
/// Easily expanded with more properties
/// </summary>
class EnteredUser
{
    public int id { get; set; }
    public string Name { get; set; }
    public int score { get; set; }
}

You can use a SortedList if you have no duplicate name.

The following code will read your input until you give a name "EXIT".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; // add this line here

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<string,int> list = new SortedList<string,int>;
            string ST_AR_NAMES;
            int IN_AR_Scores;
            while(!ST_AR_NAMES.Equals("EXIT"))
            {


                Console.WriteLine("Please enter your name ");
                ST_AR_NAMES=Console.ReadLine();
                if(!ST_AR_NAMES.Equals("EXIT"))
                {
                    Console.WriteLine("Please enter your score");
                    IN_AR_Scores = Convert.ToInt32(Console.ReadLine());

                    list.Add(ST_AR_NAMES,IN_AR_Scores);

                    for (int i = 0; i < list.Count; i++)
                    {
                        Console.WriteLine("Name: {0} - score: {1}", 
                          list.GetByIndex(i), list[list.GetByIndex(i)]);

                    }
                }
            }
        }
    }
}

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