存储用户输入的数组并显示它的队列

时间:2014-02-25 22:13:18

标签: c# arrays queue

所以我有一个简单的程序,我设置为询问用户的数组大小,然后它让他们进入元素然后打印它们,我想设置一个队列,所以它打印整个数组,例如。

历史

1 2 3 4 //数组长度

3 4 5 6 //用户猜猜本轮

此后,每次用户重新输入数组时,该数组也将显示在历史记录中。

历史

1 2 3 4 //数组长度

3 4 5 6 //用户猜猜这回合 2 6 7 8 //用户第二次输入


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

  namespace Random_test
 {
   class Program
  {
    public int n;
    public int[] UserArray;
    public int i;
    public int reEnter;
    public int rear = -1;
    public int front = -1;
    public int[] history;

    public void main()
    {


        Console.WriteLine("Please enter the Length of Your array");

        n = Convert.ToInt32(Console.ReadLine());
        UserArray = new int[n];
        history = new int[n];

        do
        {

            for (i = 0; i < n; i++)
            {
                Console.WriteLine("Your Elements");

                UserArray[i] = Convert.ToInt32(Console.ReadLine());

            }

            for (i = 0; i < n; i++)
            {
                Console.WriteLine("Your Array: {0} ", UserArray[i]);

            }
            Console.WriteLine("Would you like to re-enter your array");
            reEnter = Convert.ToInt32(Console.ReadLine());

            for (i = 0; i < 1; i++)//loop for history
            {
                insert();
                delete();
                showHistory();
            }

        } while (reEnter == 1);


    }


    public void insert()
    {


        if (rear == -1)
        {
            front = 0;
            rear++;

            history[rear] = UserArray[i];

        }
        else
        {
            rear++;
            history[rear] = UserArray[i];
        }
    }

    public void delete()
    {
        if (front == -1)
        {
            Console.WriteLine("There is no history availible");
        }
        else
        {
            front++;
        }

    }

    public void showHistory()
    {
        if (rear == -1)
        {
            Console.WriteLine("There is no history availible");

        }
        else
        {
            Console.WriteLine("History");

            for (int i = 0; i < n; i++)
            {
                Console.Write(" {0} ");
                Console.Write(" - ");
                Console.WriteLine(" {0} ", history[i]);
            }

        }
    }

    static void Main(string[] args)
    {
        Program main = new Program();
        main.main();


        Console.WriteLine("Game Over");
        Console.ReadKey();
    }
}

}


这只是一个快速启动程序,你可以看到我试图做队列,它可以工作,但每回合只打印用户数组的第一个元素。不幸的是,这是我不知道如何实现我在帖子开头谈到的内容。我想坚持这种创建队列的方法,我不想使用队列类,想法是保持它干净简单。

非常感谢。

1 个答案:

答案 0 :(得分:0)

对不起我之前的恶意评论。我认为这可能适合你的水平......快乐的编码。

public class Program
{
    static void Main(string[] args)
    {
        (new Program()).Ask();
    }

    private void Ask()
    {
        string history = "";

        while (true)
        {
            Console.Write("Len > ");
            int len = int.Parse(Console.ReadLine());

            int[] arr = new int[len];
            for (int i = 0; i < len; i++)
            {
                Console.Write("El{0} > ", i);
                arr[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine();
            string str = string.Join(",", arr);
            Console.WriteLine("Arr > {0}", str);

            Console.WriteLine("His > {0}", history);
            history += string.Format("[{0}] ", str);

            Console.WriteLine("Again?");
            if (Console.ReadLine().Length == 0)
                break;
            Console.WriteLine();
        }
    }
}
相关问题