gpt4 book ai didi

C# 控制台应用程序——尝试从 else 语句访问列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:08 25 4
gpt4 key购买 nike

这是一个数据结构和算法类。我们刚刚开始使用冒泡排序。指令是生成随机的、唯一的整数,并使用讲座中的排序技术对它们进行排序。还需要添加不同的排序技术。

为了生成随机数列表,我生成了一个列表,然后使用 fisher-yates 算法对列表进行了混洗。因此,无论我选择什么大小,我都有自己独特的排序列表。

我卡住了,因为在生成随机列表后,我无法访问列表以通过 BubbleSort 运行它。

有什么办法可以做到这一点吗?

 class Algorithms
{
static void Main(string[] args)
{

string response = "";

//Main Console Menu
while (response != "exit")
{
Console.WriteLine("Type help for list of commands");
response = Console.ReadLine();
//List<int> toSort = new List<int>();

if (response.StartsWith("exit"))
{
Environment.Exit(0);
}

else if (response.ToLower().StartsWith("help"))
{
Help(response);
}

else if (response.ToLower().StartsWith("generate"))
{

// Shuffle(Generate(response));
// have been using the line above but adding next line for
//an idea of my problem

List<int> toSort = Shuffle(Generate(response));

}

else if (response.ToLower().StartsWith("bubble"))
{
//This doesn't work and I'm trying to figure out how it can
BubbleSort(toSort);

}

}
}


//Displays help information
public static void Help(string input)
{
Console.WriteLine("\ngenerate <integer> -- Generates a data set of intended amount of integers\n"+
"algorithm <algorithm type> -- Choose which algorithm to sort data\nexit -- exit application\n" );
}

//Generates List of integers from 0 to number choosen by user
public static List<int> Generate(string size)
{
int cutString = size.Length - 9;
string sizeSubset = size.Substring(9, cutString);
List<int> numGen = new List<int>();
int dataSetSize = Convert.ToInt32(sizeSubset);
for(int i = 0; i <= dataSetSize; i++)
{
numGen.Add(i);
// Console.WriteLine(numGen[i]);

}

return numGen;

}

//Use Fisher-Yates algorithm to shuffle the list.
static Random randomize = new Random();
public static List<int> Shuffle(List<int>makeRandom)
{

List<int> shuffled = new List<int>();
int n = makeRandom.Count;
while (n > 1)
{
n--;
int k = randomize.Next(n + 1);
int value = makeRandom[k];
makeRandom[k] = makeRandom[n];
makeRandom[n] = value;
shuffled.Add(value);
Console.WriteLine(value);

}

return shuffled;

}

public static void BubbleSort(List<int>input)
{
for(int i = 0; i <= input.Count; i++)
{
for (int j = 0; j <= (input.Count - 1); j++)
{
if (input[j] > input[j + 1])
{
int temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
Console.WriteLine("hello");
}

}
}
}

}
}

最佳答案

您在 else if (response.ToLower().StartsWith("generate")) 代码块的范围内定义了列表,因此无法在该 block 之外访问它。将声明移动到 Main 方法范围,如下所示:

static void Main(string[] args)
{

string response = "";
//define your list here.
List<int> toSort = new List<int>();

//Main Console Menu
while (response != "exit")
{
Console.WriteLine("Type help for list of commands");
response = Console.ReadLine();

if (response.StartsWith("exit"))
{
Environment.Exit(0);
}

else if (response.ToLower().StartsWith("help"))
{
Help(response);
}

else if (response.ToLower().StartsWith("generate"))
{
toSort = Shuffle(Generate(response));
}

else if (response.ToLower().StartsWith("bubble"))
{

List<int> sortedList = BubbleSort(toSort);
}

}
}

关于C# 控制台应用程序——尝试从 else 语句访问列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41825062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com