c# - 变量不覆盖 C#
问题描述
我正在尝试设置一个“分数”变量,它会在我每次调用时添加一些分数时一直改变。然而,我的代码加了一些点,但如果我再次调用它,它会重新设置值并加起来。这意味着我想将一个点加到 0,它会打印 1,如果我想再次添加它,它将再次打印 1 个点。这是我的代码:
class StatusManage
{
public int score;
public void increase()
{
score++;
}
}
class QuestionManage
{
StatusManage status = new StatusManage();
public int limit = 4;
public void choose()
{
Console.WriteLine("Your score: {0}", status.score);
Questions q = new Questions();
if (status.score >= limit)
{
Console.WriteLine("The end! Congratulation");
status.score = 0;
}
switch (status.score)
{
case 0:
q.question1();
break;
case 1:
q.question2();
break;
case 2:
q.question3();
break;
}
}
public void getAnswer(char correctAnswer)
{
Console.Write("Type your answer: ");
ConsoleKeyInfo answer = Console.ReadKey();
Console.WriteLine();
if (char.ToUpper(answer.KeyChar) == correctAnswer)
{
Console.WriteLine("You are correct!");
status.increase();
choose();
}
else
{
Console.WriteLine("Incorrect answer, try it again...");
getAnswer(correctAnswer);
}
}
}
class Questions
{
QuestionManage questionManage = new QuestionManage();
public void question1()
{
// Questions
// Q1
text = "How do we declare whole number variables in C#";
string question = text + "?";
Console.WriteLine(question);
letter = 1;
text = "int 1x = 10";
string choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 2;
text = "int x = 10";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 3;
text = "float x = 10f";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 4;
text = "string x = \"10\"";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
Console.WriteLine();
questionManage.getAnswer('B');
}
public void question2()
{
// Questions
// Q2
text = "In what country is Stockholm";
string question = text + "?";
Console.WriteLine(question);
letter = 1;
text = "Germany";
string choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 2;
text = "Denmark";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 3;
text = "Austria";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 4;
text = "Sweden";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
Console.WriteLine();
questionManage.getAnswer('D');
}
public void question3()
{
// Questions
// Q2
text = "Check";
string question = text + "?";
Console.WriteLine(question);
letter = 1;
text = "Correct";
string choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 2;
text = "Incorrect";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 3;
text = "Incorrect";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
letter = 4;
text = "Incorrect";
choice = $"{choicePrefix[letter - 1]}) {text}";
Console.WriteLine(choice);
Console.WriteLine();
questionManage.getAnswer('A');
}
public static string text;
public static int letter;
public static char[] choicePrefix = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
}
谢谢你的帮助
解决方案
推荐阅读
- oracle - PL/SQL 函数中的错误 PLS-00103
- hyperledger-fabric - Hyperledger 网络存档 RuntimeException
- angular - 更新 pageIndex 时,Angular 5 Material Paginator 重置为负值
- android - Android-从Firebase实时数据库读取数据并显示在RecyclerView上
- python - 数组中每个元素的新列
- django - React Search Bar ,从另一个类调用函数
- android - 奥利奥上缺少 nmea 消息
- excel - 如何在不更改参考文献的情况下将 Excel 工作表复制并粘贴到新书中?
- php - 插入多个复选框值php ajax
- javascript - 无法从文本区域字段插入数据 - mysql