我是C#编程的新手,我遇到了问题。我不知道在哪里放置我的函数以及如何声明它们以便我可以从我的switch语句中调用它们。我能否在我的函数中使用我的numberarr和wordarr数组,或者我还需要为它创建一个单独的函数这是我的代码:
class Program
{
enum Menu
{
Numbers = 1,
Words = 2,
Exit = 3,
}
static void Main(string[] args)
{
bool isValid;
do
{
isValid = true;
Menu menu = 0;
int number;
string word;
Console.WriteLine("Choose an option from the menu: ");
Console.WriteLine("1. Numbers ");
Console.WriteLine("2. Words ");
Console.WriteLine("3. Exit ");
switch (menu)
{
case Menu.Numbers:
List<int> numberarr = new List<int>();
Console.WriteLine("Please input as many numbers as you like or type exit");
number = int.Parse(Console.ReadLine());
numberarr.Add(number);
break;
case Menu.Words:
List<string> wordarr = new List<string>();
Console.WriteLine("Please input as many numbers as you like");
word = Console.ReadLine();
wordarr.Add(word);
break;
case Menu.Exit:
break;
default:
Console.WriteLine("You have made an invalid selection, try again");
isValid = false;
break;
}
} while (isValid);
}
}
class Choice
{
static void Numbers(int sum, int count, int average, int max, int min)
{
}
static void Words(string[] args)
{
}
static void Exit()
{
}
}
答案 0 :(得分:0)
如果我理解你的问题,只需将你的功能放在你的课堂上。我已相应调整了您的代码。您的word / num类也可能有问题。你通常必须实例化它们,但是像Choice myChoice = new Choice(); 课程计划 { 枚举菜单 { 数字= 1, 字= 2, 退出= 3, }
static void Main(string[] args)
{
bool isValid;
do
{
isValid = true;
Menu menu = 0;
int number;
string word;
Console.WriteLine("Choose an option from the menu: ");
Console.WriteLine("1. Numbers ");
Console.WriteLine("2. Words ");
Console.WriteLine("3. Exit ");
switch (menu)
{
case Menu.Numbers:
List<int> numberarr = new List<int>();
Console.WriteLine("Please input as many numbers as you like or type exit");
number = int.Parse(Console.ReadLine());
numberarr.Add(number);
int retInt = functionGetInt(number)
break;
case Menu.Words:
List<string> wordarr = new List<string>();
Console.WriteLine("Please input as many numbers as you like");
word = Console.ReadLine();
wordarr.Add(word);
string retString = functionGetString(word);
break;
case Menu.Exit:
break;
default:
Console.WriteLine("You have made an invalid selection, try again");
isValid = false;
break;
}
} while (isValid);
private string functionGetString(string pParmString)
{
//code
return "string";
}
private int functionGetInt(int pParmInt)
{
//code
return 0;
}
}
}
答案 1 :(得分:0)
您不能使用Main中Choice
类中定义的方法,因为您尚未使用public
标识符声明它们。在C#类属性中,默认为private
,所以除非你明确地将它们声明为public
,否则只有类本身才会知道它们的存在。
所以基本上只需将Choice
中的所有声明从static void MethodName
更改为public static void MethodName
,然后您就可以从Choice
类中调用它们,例如; < / p>
Choice.Exit();
编辑:您还需要进行一些更改以使switch语句有效。正如评论中指出的那样,menu
无法获得除0以外的值。我建议你使用更像下面的内容;
isValid = true;
int menu = 0;
int number;
string word;
Console.WriteLine("What type do you want to use?");
Console.WriteLine("Press 1 for numbers, 2 for words, or 3 exit.");
string input = Console.ReadLine(); // we must read the users input
if (!int.TryParse(input, out menu))
{
// the user didn't enter a number make them try again
// note you might want to use a loop here to ensure the program does not
// proceed until the user has entered "1", "2", or "3"
}
switch (menu)
答案 2 :(得分:0)
编辑:
对于你在这里提出的代码,这将是一种可能性。我删除了枚举,我通常会尝试处理所提供的代码但是没有必要:
class Program
{
//enum Menu
//{
// Numbers = 1,
// Words = 2,
// Exit = 3,
//}
static void Main(string[] args)
{
bool isValid;
do
{
isValid = true;
int menu = 0;
int[] number;
string word;
Console.WriteLine("Choose an option from the menu: ");
Console.WriteLine("1. Numbers ");
Console.WriteLine("2. Words ");
Console.WriteLine("3. Exit ");
string s = Console.ReadLine();
while (!Regex.IsMatch(s, "^[1-3]{1}$"))
{
Console.WriteLine("Please enter a valid choice(1 to 3)");
s = Console.ReadLine();
}
menu = Convert.ToInt32(s);
switch (menu)
{
case 1:
List<int> numberarr = new List<int>();
Console.WriteLine("Please input as many numbers as you like separeted by space or comma,or type exit");
string numbers = Console.ReadLine();
if (numbers == "exit")
Choice.Exit();
else
{
number = numbers.Split(new char[] { ',', ' ' }).Select(x => int.Parse(x)).ToArray();
numberarr.AddRange(number);
Choice.Numbers(numberarr.Sum(), numberarr.Count, numberarr.Average(), numberarr.Max(), numberarr.Min());
}
break;
case 2:
List<string> wordarr = new List<string>();
Console.WriteLine("Please input as many numbers as you like separeted by space or comma");
word = Console.ReadLine();
wordarr.AddRange(word.Split(new char[] { ',', ' ' }));
Choice.Words(wordarr);
break;
case 3:
Choice.Exit();
break;
default:
Console.WriteLine("You have made an invalid selection, try again");
isValid = false;
break;
}
} while (isValid);
Console.ReadKey();
}
}
class Choice
{
public static void Numbers(int sum, int count, double average, int max, int min)
{
int a = sum;
int b = count;
double c = average;
int d = max;
int e = min;
//just as example.
}
public static void Words(List<string> args)
{
//do whatever you need here
}
public static void Exit()
{
Environment.Exit(0);
}
}