简体   繁体   English

C# 数组帮助(我是初学者):)

[英]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:目前此代码要求用户 ONCE 输入他们的姓名和分数,然后在屏幕上输出:

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?我如何让这段代码要求用户输入/输出他们的名字和分数……10 次?

You need to take your for loop and include the rest of your code as such:您需要使用for循环并将其余代码包括在内:

    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.我已将其配置为循环 10 次。 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.如果您想输入用户数量,并为每个用户跟踪一个 id。 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.如果您没有重复的名称,则可以使用 SortedList。

The following code will read your input until you give a name "EXIT".以下代码将读取您的输入,直到您指定名称“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)]);

                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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