C#array help(我是初学者):)

时间:2015-11-28 00:45:57

标签: c#

目前此代码要求用户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();
    }
  }
}

如何制作此代码,要求用户输入/输出他们的名字和分数...... 10次?

3 个答案:

答案 0 :(得分:0)

您需要执行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;

    }

我已将其配置为循环10次。你的初始帖子有点模糊。这有帮助吗?

答案 1 :(得分:0)

如果您想输入用户数,并跟踪每个用户的ID。添加了评论

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; }
}

答案 2 :(得分:0)

如果您没有重复名称,则可以使用SortedList。

以下代码将读取您的输入,直到您输入名称“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)]);

                    }
                }
            }
        }
    }
}