我在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():并非所有代码路径都返回一个 值“
答案 0 :(得分:1)
以下是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()
撰写的内容将导致无限递归调用 - &gt;计算器