我想首先将每个if语句分隔成单独的函数,然后确保菜单循环直到有效输入为止。
有人可以就如何解决这个问题给我一些好的建议吗?
我在以下行上也有编译时错误:
for(int i = 0; i < integer_array.length; i++)
我似乎无法弄明白为什么。
这是我的完整代码
int option;
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Entre a number to Navigate");
Console.WriteLine();
Console.WriteLine("1).Run Random Number Generator");
Console.WriteLine("2).Open Help Menu");
Console.WriteLine("3).Generate random numbers from a specific range. ");
Console.WriteLine("4).Exit ");
option= Convert.ToInt16(Console.ReadLine()); // Input by user which if statment to run
if (option == 1)
{
Console.Clear();
Console.WriteLine(" --- Random Number Generator --- ");
Random rand_number = new Random(); // creates a randon number
HashSet<int> numbers = new HashSet<int>();
while (numbers.Count < 6)
{
int new_number = rand_number.Next(1, 50);
if (!numbers.Contains(new_number))
numbers.Add(new_number);
} // creates 6 ramdon numbers between 1 and 50 and removes duplicate
int[] integer_array = numbers.ToArray(); // puts number in array
Array.Sort(integer_array); // sorts the array in assending order.
Console.WriteLine(integer_array[0]); // Displays the first element of array
Console.WriteLine(integer_array[1]); // Displays the Second element of array
Console.WriteLine(integer_array[2]); // Displays the third element of array
Console.WriteLine(integer_array[3]);
Console.WriteLine(integer_array[4]);
Console.WriteLine(integer_array[5]);
Console.WriteLine();
Console.WriteLine("Press any key to Exit");
}
else if (option == 2)
{
Console.Clear();
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Help Menu");
Console.WriteLine();
Console.WriteLine("To navigate through the program type in the number related to the task then press enter.For example To exit the program press 4 and then press enter.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to Exit");
}
else if (option == 3)
{
int min, max, howMany;
Console.Clear();
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Entre the Minmun Value");
min =Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Entre the Maximin Value");
max = Convert.ToInt16(Console.ReadLine())+1;
Console.WriteLine("How many random number you want to generate");
howMany = Convert.ToInt16(Console.ReadLine());
Console.Clear();
Random rand_number = new Random(); // creates a randon number
HashSet<int> numbers = new HashSet<int>();
while (numbers.Count < howMany)
{
int new_number = rand_number.Next(min, max);
if (!numbers.Contains(new_number))
numbers.Add(new_number);
} // creates howMany (input by user) ramdon numbers between min (input by user) and max (input by user)and removes duplicates
int[] integer_array = numbers.ToArray(); // puts number in array
Array.Sort(integer_array); // sorts the array in assending order.
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < integer_array.length; i++) // this loop gives error length is under lined with red squiggly line
{
Console.WriteLine(integer_array[i]);
} // Displays the elements of array
Console.WriteLine("Press any key to Exit");
}
else if (option == 4)
{
Console.WriteLine("Press any key to Exit");
}
else
{
Console.WriteLine("Incorrect option slected press any key to Exit");
}
Console.ReadKey();
答案 0 :(得分:0)
首先,你的for循环结束(给你一个错误的那个)。包含拼写错误的财产。
integer_array.length
实际应该是
integer_array.Length
&#39;长度&#39;财产是&#39;长度&#39;在C#
确保在按下任何正确的输入键之前循环代码。 您可以使用
封装整个代码体while(true) {
// Your code here
}
然后确保最后;在每个if语句中。您应该添加关键字
break;
当然除了最后一个。
最后一件事。 为了将每个if语句分成它们自己的函数,你所要做的就是......(例如)
更改以下代码
else if (option == 2)
{
Console.Clear();
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Help Menu");
Console.WriteLine();
Console.WriteLine("To navigate through the program type in the number related to the task then press enter.For example To exit the program press 4 and then press enter.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to Exit");
}
向
else if (option == 2)
{
OpenHelpMenu();
}
然后在文件的末尾;在最后两个右括号} 之前 添加新功能
void OpenHelpMenu() {
Console.Clear();
Console.WriteLine(" --- Random Number Generator --- ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Help Menu");
Console.WriteLine();
Console.WriteLine("To navigate through the program type in the number related to the task then press enter.For example To exit the program press 4 and then press enter.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to Exit");
}
对每个if语句重复,你应该没问题: - )