解决方案需要与家庭作业c# (cs7036)



我正试图在用户可以选择一个选项的位置上制作一个具有不同选项的菜单。我在单独的文件上制作选项,所以我以后可以从这些文件调用方法,而不是在主文件中编写所有内容。其中一个菜单选项将有一个随机生成器的选项,它将生成一个随机字符串。我在另一个文件上创建了这个方法,但是当我试图将它调用到主方法时,它给了我一个错误(7036)。我在下面粘贴了一个示例代码,但您也可以访问并运行此链接上的代码:https://replit.com/@AY2002/testc22#main.cs,这将更容易理解。我是一个乞丐,所以我在寻找一个简单的答案。谢谢你!

//MAIN replit FILE
using System;
using System.Collections.Generic;
namespace Namespace1 {
// This is the main file where the menu is built. the menu is working fine. 
// the menu have 4 options and an exit option, which will be divided into 4 different replit files and one of them will have a method that randomly generates a string. you can see the method when you scroll down near to the bottom of the main file.

class Program {
public static void Main (string[] args) 
{

string[] Menuchoises = new string [] {"Choise1","Choise2","Choise3","Choise4","Choise5"};

int x = 0;

while (true){
Console.Clear();

Console.WriteLine("welcome to menu");
Console.WriteLine();
Console.CursorVisible = false;

if(x == 0) {
Console.WriteLine(" " + Menuchoises[0] + "  {");
Console.WriteLine(Menuchoises[1]);
Console.WriteLine(Menuchoises[2]);
Console.WriteLine(Menuchoises[3]);
Console.WriteLine(Menuchoises[4]);
}
else if(x == 1) {
Console.WriteLine(Menuchoises[0]);
Console.WriteLine(" " + Menuchoises[1] + "  {");
Console.WriteLine(Menuchoises[2]);
Console.WriteLine(Menuchoises[3]);
Console.WriteLine(Menuchoises[4]);
}
else if(x == 2) {
Console.WriteLine(Menuchoises[0]);
Console.WriteLine(Menuchoises[1]);
Console.WriteLine(" " + Menuchoises[2] + "  {");
Console.WriteLine(Menuchoises[3]);
Console.WriteLine(Menuchoises[4]);
}
else if(x == 3) {
Console.WriteLine(Menuchoises[0]);
Console.WriteLine(Menuchoises[1]);
Console.WriteLine(Menuchoises[2]);
Console.WriteLine(" " + Menuchoises[3] + "  {");
Console.WriteLine(Menuchoises[4]);
}
else if(x == 4) {
Console.WriteLine(Menuchoises[0]);
Console.WriteLine(Menuchoises[1]);
Console.WriteLine(Menuchoises[2]);
Console.WriteLine(Menuchoises[3]);
Console.WriteLine("t" + Menuchoises[4] + "  {");
}


var key = Console.ReadKey();

if (key.Key == ConsoleKey.DownArrow && x != Menuchoises.Length -1) {
x++;
}
else if (key.Key == ConsoleKey.UpArrow && x>=1) {
x--; 
} else if (key.Key == ConsoleKey.Enter) {

switch (x) {  
case 0:
Menuchoise1();
break;
case 1:
Menuchoise2();
break;
case 2: 
Menuchoise3();
break;
case 3:
Menuchoise4();
break;
case 4:
Menuchoise5();
break;
}  
}
}
}

public static void Menuchoise1() {
// Class2.second is the name of the second class which will be the method that will appear when you choose the 1st option in the menu. 
// The second class is in the second file which you`ll see below the main file
// The CS 7036 error seems to be  appearing here
Class2.second();
Console.Clear(); 

Console.ReadKey();
}

public static void Menuchoise2() {



Console.Clear();

Console.ReadKey();
}
public static void Menuchoise3() {
Console.Clear();

Console.ReadKey();


}
public static void Menuchoise4() {



Console.Clear();

Console.ReadKey();
}
public static void Menuchoise5() {

Console.Clear();

Console.WriteLine("press enter to exit the menu");
Console.ReadKey();
Console.Clear();
Environment.Exit(1);

}

}
}

// SECOND replit FILE 
//this is the second file where i have the random value generator. 

using System; 
using System.Collections.Generic;
namespace Namespace1 { 
// class name of the second file
public class Class2 {
// the string[]args function that will later be put in the main file in order to use this method in the menu
public static string second(string[] Random) {
string[] RandomChoises = new string [4];
// list on options which will be randomly generated
RandomChoises[0] = "C1";
RandomChoises[1] = "C2";
RandomChoises[2] = "C3";
RandomChoises[3] = "C4";
RandomChoises[4] = "C5";

for (int i = 0; i < RandomChoises.Length; i++)
{
Console.WriteLine(RandomChoises[i]);
}


// the choises are randomly generated here
Random rnd = new Random();
int Randomanswer = rnd.Next(1,RandomChoises.Length);
Console.WriteLine("You got the answer: " + RandomChoises[Randomanswer]);
return Convert.ToString(Randomanswer);


}
}
}

正如在注释中提到的,问题是second方法接受一个参数,但没有使用一个参数调用。由于没有使用该参数,因此应该删除它。稍微清理一下类应该会得到如下内容:

public static class RandomHelpers{
public static string GetRandomValue() {

// use collection initializer
string[] choices= new []{"C1","C2","C3","C4","C5"}

// use foreach loop
foreach(var choice in choices){
Console.WriteLine(choice );
}

// You should probably not recreate the random object for each method call
// But this should work fine for demonstration purposes
Random rnd = new Random();
// start from zero
int randomIndex = rnd.Next(0,RandomChoises.Length);
var randomValue = choices[randomIndex];
// You should probably just return the random value,
// and let the caller print it to console.
Console.WriteLine("You got the answer: " + randomValue );

// Either return the index as an int, or the value as a string
// Do not convert numbers to strings unless you are writing to the console/file
return randomValue ;
}
}

这应该工作得更好。随着经验的积累,你应该找到更好的方法将功能拆分为可重用的方法。

您在second.cs中定义数组的方式有问题。要么需要显式地说明数组将包含多少个元素,像这样:

string[] RandomChoice = new string[5];

或者你可以不输入数字,让编译器从{}中输入的元素数量中推断出来,像这样:

string[] RandomChoises = new string[] { "C1", "C2", "C3", "C4", "C5" };

你可以在这里阅读更多关于在c#中声明数组的细节:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/

修复后,请参阅JonasH回答关于second()中方法参数的问题。

最新更新