C# 在方法之间传递变量
我的程序中有两种方法。 1.开考 2.分数考试 如你所见, 在第一种方法中,我计算了有多少问题是正确的或错误的 第二,我想展示结果 问题: 如何传递正确答案和错误答案以及方法之间的列表 这就是我所拥有的:C# 在方法之间传递变量,c#,methods,C#,Methods,我的程序中有两种方法。 1.开考 2.分数考试 如你所见, 在第一种方法中,我计算了有多少问题是正确的或错误的 第二,我想展示结果 问题: 如何传递正确答案和错误答案以及方法之间的列表 这就是我所拥有的: static void Main(string[] args) { int menuChoice = 99; Question[] questions = new Question[30]; do {
static void Main(string[] args)
{
int menuChoice = 99;
Question[] questions = new Question[30];
do
{
Console.Clear();
DisplayMenu();
menuChoice = InputOutput_v1.GetValidInt("Option");
switch (menuChoice)
{
case 1:
InputOutput_v1.DisplayTitle("Start Exam");
CreateQuestion(questions);
break;
case 2:
InputOutput_v1.DisplayTitle("Mark Exam");
DisplayAllQuestions(questions);
break;
}
} while (menuChoice != '0') ;
}
public static void StartExam(Question[] questions)
{
char[] studentAnswers = new char[30];
int[] wrongAnswers = new int[30];
int correctlyAnswered = 0;
int falselyAnswered = 0;
string list = "";
Console.Clear();
Console.WriteLine("== DO NOT CHEAT! ==\n");
Console.WriteLine("----------------");
for (int i = 0; i < studentAnswers.Length; i++)
{
Console.WriteLine("\nQuestion {0}", i + 1);
Console.WriteLine("------------");
questions[i].DisplayQuestion();
studentAnswers[i] = InputOutput_v1.GetValidChar("Your Answer (A, B, C, D)");
if (studentAnswers[i] == questions[i].correctAnswer)
{
correctlyAnswered = correctlyAnswered + 1;
}
else
{
falselyAnswered = falselyAnswered + 1;
wrongAnswers[i] = i + 1;
list += i + 1 + ", ";
}
}
}
public static void MarkExam(Question[] questions)
{
if (correctlyAnswered >= 15)
{
Console.WriteLine("Passed with {0} correct answers", correctlyAnswered);
}
else
{
Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: {1} ", falselyAnswered, list.Remove(list.Length - 2));
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
int menuChoice=99;
问题[]问题=新问题[30];
做
{
Console.Clear();
显示菜单();
menuChoice=InputOutput_v1.GetValidInt(“选项”);
开关(menuChoice)
{
案例1:
InputOutput_v1.DisplayTitle(“开始考试”);
问题;
打破
案例2:
输入输出v1.显示标题(“分数考试”);
显示所有问题(问题);
打破
}
}while(menuChoice!=“0”);
}
公共静态无效StartExam(问题[]问题)
{
char[]studentAnswers=新字符[30];
int[]错误答案=新int[30];
int正确回答=0;
int错误回答=0;
字符串列表=”;
Console.Clear();
Console.WriteLine(“==不要欺骗!==\n”);
Console.WriteLine(“------------------------------”);
for(int i=0;i=15)
{
WriteLine(“传递了{0}个正确答案”,正确回答);
}
其他的
{
Console.WriteLine(“由于{0}个错误答案而失败。错误答案为:{1}”,错误答案为,list.Remove(list.Length-2));
}
Console.ReadLine();
}
将正确答案、错误答案和列表定义为方法之外的全局变量,例如
private int correctlyAnswered;
private int falselyAnswered;
private Question[] questions;
将正确答案、错误答案和列表定义为方法之外的全局变量,例如
private int correctlyAnswered;
private int falselyAnswered;
private Question[] questions;
首先,为什么要使用数组而不是列表?在这个类中,您的方法实际上不需要是静态的 其次,为类创建私有变量,它们可以在所有方法中使用
public class ExamClass
{
private List<Question> questions = new List<Question>();
private List<Question> incorrect = new List<Question>();
private List<Question> correct = new List<Question>();
...
}
公共类ExamClass
{
私人列表问题=新列表();
私有列表不正确=新列表();
私有列表正确=新列表();
...
}
首先,为什么要使用数组而不是列表?在这个类中,您的方法实际上不需要是静态的
其次,为类创建私有变量,它们可以在所有方法中使用
public class ExamClass
{
private List<Question> questions = new List<Question>();
private List<Question> incorrect = new List<Question>();
private List<Question> correct = new List<Question>();
...
}
公共类ExamClass
{
私人列表问题=新列表();
私有列表不正确=新列表();
私有列表正确=新列表();
...
}
那么,为什么不能调用MarkExam()方法
public static void StartExam(Question[] questions)
{
char[] studentAnswers = new char[30];
int[] wrongAnswers = new int[30];
int correctlyAnswered = 0;
int falselyAnswered = 0;
string list = "";
........
MarkExam(questions, wrongAnswers, correctlyAnswered);
在这种情况下,您必须相应地更改您的方法签名那么,您为什么不能调用您的MarkExam()
方法来传递这些变量呢
public static void StartExam(Question[] questions)
{
char[] studentAnswers = new char[30];
int[] wrongAnswers = new int[30];
int correctlyAnswered = 0;
int falselyAnswered = 0;
string list = "";
........
MarkExam(questions, wrongAnswers, correctlyAnswered);
在这种情况下,您必须相应地更改方法签名我如何将它们传递到方法中?它将是publicstaticvoidstartexam(问题[]问题,int纠正答案,int错误答案,字符串列表)?使用全局变量,您不需要将它们传递到方法中,只需在方法之外声明变量即可。然后您可以在类中的任何方法中访问这些变量。如何将它们传递到方法中?它将是publicstaticvoidstartexam(问题[]问题,int纠正答案,int错误答案,字符串列表)?使用全局变量,您不需要将它们传递到方法中,只需在方法之外声明变量即可。然后可以在类内的任何方法中访问这些变量。