为算法创建和选择数组



我正在用 C# 制作一个控制台应用程序,我从这种语言开始,所以我有一些障碍。 我的任务是创建一个应用程序,它将允许您选择一种准备好的算法并使用它进行一些计算。 我必须创建三种类型的数组,随机数组、自定义数组和预定义数组。然后我需要让用户选择其中一个并在其他算法中使用它。 例如,我需要计算数组中元素的总和。 用户应选择数组,然后按另一个按钮运行算法。

到目前为止,我有这个:

namespace Menu
{
static class MenuProste
{
public static void StartMenuProste()
{
Console.Title = "Moje menu";
while (true)
{
Console.Clear();
Console.WriteLine(">>> Witaj! Wciśnij klawisz, aby wybrać algorytm! <<<");
Console.WriteLine("1- Wybierz zdefiniowany wcześniej ciąg.");
Console.WriteLine("2- Zdefiniuj własny ciąg.");
Console.WriteLine("3- Wybierz losowy ciąg.");
Console.WriteLine("4- Suma elementów ciagu n-elementowego.");
Console.WriteLine("5- Iloczyn elementów ciagu n-elementowego.");
Console.WriteLine("6- Srednia arytmetyczna elementów ciagu n-elementowego.");
Console.WriteLine("7- Wypisanie elementów ciagu Fibonacciego");
Console.WriteLine("8- Ile razy dany element podany na wejsciu wystepuje w danym ciagu n-elementowym. ");
Console.WriteLine("9- Znajdowanie elementu najwiekszego (najmniejszego) w danym ciagu n-elementowy.");
Console.WriteLine("0- Koniec");
int[] tab = null;
ConsoleKeyInfo klawisz = Console.ReadKey();
switch (klawisz.Key)

{
case ConsoleKey.D1:
Console.Clear();
tab = PreDefinedArray();
break;
case ConsoleKey.D2:
Console.Clear();
tab = UserDefinedArray();
opcjaWBudowie();
break;
case ConsoleKey.D3:
Console.Clear();
tab = RandomArray();
//average.Execute(tab);
break;
case ConsoleKey.D4:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.D5:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.D6:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.D7:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.D8:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.D9:
Console.Clear();
opcjaWBudowie();
break;
case ConsoleKey.Escape:
case ConsoleKey.D0:
Environment.Exit(0); break;
default: break;
}
}
}
private static int[] RandomArray()
{;
Random RandomArray = new Random();
int number = RandomArray.Next(1, 100);
System.Console.WriteLine(number);
Console.ReadKey();

}
private static int[] UserDefinedArray()
{
throw new NotImplementedException();
}
private static int[] PreDefinedArray()
{
int[] Array = new int[6] {4, 8, 15, 16, 23, 42 };
return PreDefinedArray();
}
static void opcjaWBudowie()
{
Console.Write("Opcja w budowie!");
Console.ReadKey();
}
}

}

算法位于同一解决方案中,位于不同的项目文件中。我必须将它们全部结合起来。 任何人都可以帮助我编写这些数组,然后帮助我修改我的代码,以便它将使用这些新数组吗? 这是我的平均算法,计算您将输入的数字的平均值

namespace AverageNums

{
class average
{
public static void Execute(int[] tab)
{
int iloscLiczb = 0;  //Zmienne, wyzerowane na początku
double suma = 0;
Console.Write("Ile liczb chcesz zawrzeć w średniej: "); //Otrzymuję ile liczb będzie branych pod uwagę, int32.Parse konwertuje je na int.(liczbę całkowitą)
iloscLiczb = Int32.Parse(Console.ReadLine());
for (int i = 0; i < iloscLiczb; i++) //pętla obliczająca
{
Console.Write("Podaj liczbę: "); //Liczby wpisywane przez użytkownika, suma = suma + liczba
suma += double.Parse(Console.ReadLine());
}
Console.WriteLine("Średnia arytmetyczna z podanych liczb wynosi: " + suma / iloscLiczb);
Console.ReadKey();
}
}
}

还有我的代码来自元素总和算法:

namespace sum
{
class Sum
{
//Suma elementów ciągu n-elementowego
public void Main(string[] args)
{
int wynik = 0; //Wynik wynosi 0
int[] zbior = new int[] { 4, 8, 15, 16, 23, 42 };  //Zbiór liczb ciągu, + pozycji, zaczyna się od i[0]=4
for (int i = 0; i < zbior.Length; i++) //Rozpoczęcie pętli, dla i o indexie 0, które jest mniejsze od ilościu elementów zbioru, i zwiększa się o:
{
wynik += zbior[i];   // Wynik  + 4 + 8 + 15 + 16 
}
Console.WriteLine($"Wynik sumy elementów ciągu wynosi: {wynik.ToString()}");
Console.ReadKey();
}
}
}

如何更改代码以使其使用预定义的数组?我收到一个错误

"private static int[] RandomArray() :并非所有代码路径都返回 值">

这是RandomArray()部分的版本,带有UserDefinedArray()提示:

private static int[] RandomArray()
{
Random r = new Random();
// define size of array (here random between 5 and 30)
// so for user defined array, maybe ask user the size he wants
int size = r.Next(5,30);
// create the array
int[] array = new int[size];
// fill the array
for(int i=0; i<size; i++)
{
// for user defined, ask for every value
array[i] = r.Next(1,100);
}
return array;
}

另请注意,您的PreDefinedArray()应该更像:

private static int[] PreDefinedArray()
{
int[] array = new int[6] {4, 8, 15, 16, 23, 42 };
return array;
}

你用return PreDefinedArray()写的东西将导致无限递归调用 --> 堆栈溢出

最新更新