【问题标题】:Queue that stores User's entered Array and displays it存储用户输入的数组并显示它的队列
【发布时间】:2014-02-25 22:13:18
【问题描述】:

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

历史

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

}


这只是一个快速记下的程序,正如您所看到的,我已尝试执行队列,它可以工作,但每轮仅打印用户数组的第一个元素。不幸的是,这是我不知道如何实施我在帖子开头谈到的内容的地方。我想坚持这种创建队列的方法,我不想使用队列类,我的想法是保持它的干净和简单。

非常感谢。

【问题讨论】:

  • 很抱歉打断你,但这里没有什么干净也不简单的:/
  • @MikkoViitala 什么是我最好的选择,当我说干净和简单时,我的意思是不使用特殊的 C# 运算符或方法。
  • 最好的选择是重新思考和重做,一条评论太多了。

标签: c# arrays queue


【解决方案1】:

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

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

【讨论】:

  • 没关系,谢谢,我明天去试试。几天来一直试图让这个工作。但我猜这只是一个学习过程。
  • 是的,这是我出于显示目的所需要的,但想法是它每次都存储历史记录,以便建立历史记录。那是我被卡住了。
  • 我假设我有最后一个问题,有没有一种方法可以在不使用 .join 和 .format 的情况下做到这一点,因为我宁愿知道它是如何工作的,也不愿使用特殊的 c# 方法。
  • 当然,您可以通过多种方式做事。但这些不是“特殊”而是非常基本的方法。通过加入,您可以例如从数组中获取人类可读的格式,并使用 Format 定义您希望如何格式化内容。如果你只使用数组,它会更复杂,更容易出错,更不用说可读性了。
猜你喜欢
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 2021-03-20
  • 1970-01-01
相关资源
最近更新 更多