【发布时间】: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# 运算符或方法。
-
最好的选择是重新思考和重做,一条评论太多了。