我正在尝试在C#中创建一个控制台应用程序,要求用户先登录,然后进行处理,只回答每个用户的指定问题。这样做的最佳方式是什么?我有四个问题,我只想向第一个用户询问前两个问题,而第二个用户要求另外两个问题。
以下是我的代码:
static void Main(string[] args)
{
String username;
String password;
int row;
string[,] accnts = { { "jack", "111", "1" }, { "ibo", "121", "2" } };
Console.Write("enter username >> ");
username = Console.ReadLine();
Console.Write("enter password >> ");
password = Console.ReadLine();
for (row = 0; row < 3; row++)
{
if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row, 0]);
}
else if (username.Equals(accnts[row + 1, 0]) && password.Equals(accnts[row + 1, 1]))
{
Console.WriteLine("welcome " + accnts[row + 1, 0]);
}
else
{
Console.WriteLine("invalid access");
break;
}
string[,] question_id = { { "1", "1" }, { "1", "2" }, { "2", "3" }, { "2", "4" } };
string[,] questions = { { "Türkiyenin baskenti neresidir?", "1" }, { "Baskomutan kim?", "2" }, { "2 kere 2?", "3" }, { "when did the world war 1 start?", "4" } };
string[,] Answers = { { "a)ankara b)istanbul c)izmir", "1" }, { "a)ismet b)Atatürk c)Ali ", "2" }, { " a)1 b)2 c)4 ", "3" }, { " a)1912 b)1914 c)1915", "4" } };
string[,] trueAnswers = { { "a", "1" }, { "b", "2" }, { "c", "3" }, { "c", "4" } };
int result = 0;
string answers = "";
for (int i = 0; i < questions.GetLength(0); i++)
{
Console.WriteLine(questions[i, 0]);
Console.WriteLine("--------------------------");
for (int y = 0; y < Answers.GetLength(0); y++)
{
if (Answers[y, 1] == questions[i, 1])
{
Console.WriteLine(Answers[y, 0]);
answers = Console.ReadLine();
for (int z = 0; z < trueAnswers.GetLength(0); z++)
{
if (trueAnswers[z, 1] == questions[i, 1])
{
if (trueAnswers[z, 0] == answers)
result = result + 10;
Console.WriteLine("total is " + result);
}
}
}
}
}
if (result < 20)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
return;
}
}
答案 0 :(得分:1)
首先让我告诉你完全错误的做法。这是我能做的最脏,最快的修复:
static void Main(string[] args)
{
String username;
String password;
int row;
string[,] accnts = { { "jack", "111", "1" }, { "ibo", "121", "2" } };
Console.Write("enter username >> ");
username = Console.ReadLine();
Console.Write("enter password >> ");
password = Console.ReadLine();
for (row = 0; row < 3; row++)
{
if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row, 0]);
}
else if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row + 1, 0]);
}
else
{
Console.WriteLine("invalid access");
// changed break to continue, because it was crashing
continue;
}
string[,] question_id = { { "1", "1" }, { "1", "2" }, { "2", "3" }, { "2", "4" } };
string[,] questions = { { "Türkiyenin baskenti neresidir?", "1" }, { "Baskomutan kim?", "2" }, { "2 kere 2?", "3" }, { "when did the world war 1 start?", "4" } };
string[,] Answers = { { "a)ankara b)istanbul c)izmir", "1" }, { "a)ismet b)Atatürk c)Ali ", "2" }, { " a)1 b)2 c)4 ", "3" }, { " a)1912 b)1914 c)1915", "4" } };
string[,] trueAnswers = { { "a", "1" }, { "b", "2" }, { "c", "3" }, { "c", "4" } };
int result = 0;
string answers = "";
// here I've added a start and end thingy to offset the loop acording to logged user.
int start = 0;
int endModifier = 2;
if (username == accnts[1,0])
{
start = 2;
endModifier = 0;
}
for (int i = start; i < questions.GetLength(0) - endModifier; i++)
{
Console.WriteLine(questions[i, 0]);
Console.WriteLine("--------------------------");
for (int y = 0; y < Answers.GetLength(0); y++)
{
if (Answers[y, 1] == questions[i, 1])
{
Console.WriteLine(Answers[y, 0]);
answers = Console.ReadLine();
for (int z = 0; z < trueAnswers.GetLength(0); z++)
{
if (trueAnswers[z, 1] == questions[i, 1])
{
if (trueAnswers[z, 0] == answers)
result = result + 10;
Console.WriteLine("total is " + result);
}
}
}
}
}
if (result < 20)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
return;
}
}
当然你想要最好的方法来做,我会使用OOP(面向对象的编程) 首先,我将创建一些帮助类来保存数据: 帐户的内部类......
class Account
{
public string UserName { get; set; }
public string Password { get; set; }
public int Group { get; set; }
}
问题的内部类:
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
}
最后一个答案问题可以帮助我们解决逻辑问题:
class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
填写这些类很简单,它几乎与您的示例相同,但名称更加精美。 List在这里是你的朋友,你可以在字段中存储所有帐户但没有那些讨厌的索引https://msdn.microsoft.com/cs-cz/library/6sh2ey19(v=vs.110).aspx#Anchor_8
List<Account> accountsList = new List<Account>();
accountsList.Add(new Account { UserName = "jack", Password = "111", Order = 1 });
accountsList.Add(new Account { UserName = "ibo", Password = "121", Order = 2 });
将长代码拆分为小方法也很好,因此此方法将检查提供的用户名和密码是否正确并返回true或false。
private static bool CheckUserPassword(List<Account> accountsList, string username, string password)
{
foreach (Account account in accountsList)
{
if (account.UserName == username)
{
if (account.Password == password)
{
Console.WriteLine("welcome " + account.UserName);
return true;
}
else
{
Console.WriteLine("invalid access");
return false;
}
}
}
return false;
}
通过帐户,我还创建了一种方法来填充测验中的所有答案,在那里您可以清楚地了解如何在需要时添加更多问题:
private static List<Question> FillQuestions()
{
List<Question> questionList = new List<Question>();
List<Answer> answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "Ankara", IsCorrect = true, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Istambul", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Izmir", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Türkiyenin baskenti neresidir?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "ismet", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Atatürk", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Ali", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Baskomutan kim?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "2", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "4", IsCorrect = true, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "2 kere 2?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1912", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "1914", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "1915", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "When did the world war 1 start?", AnswersList = answerList });
return questionList;
}
使用此框架,您可以通过向Question类添加属性来简单地实现哪些问题:
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
// added property
public int DesiredGroup { get; set; }
}
并为FillQuestions()方法中的哪个组分配哪些问题: //码... questionList.Add(新问题{QuestionText =“世界大战1何时开始?”,AnswersList = answerList,DesiredGroup = 2}); //代码...
然后过滤问题循环中的每个问题:
if (questions[i].DesiredGroup == accountsList.Find(x => x.UserName == username).Group)
{
continue;
}
如果可能有点复杂,但替代方案是使用CheckUserPassword方法返回帐户,或者将其输出参数...这是非常多的信息,所以我保持原样。
现在完成的代码如下所示:
static void Main(string[] args)
{
List<Account> accountsList = new List<Account>();
accountsList.Add(new Account { UserName = "jack", Password = "111", Group = 1 });
accountsList.Add(new Account { UserName = "ibo", Password = "121", Group = 2 });
Console.Write("enter username >> ");
string username = Console.ReadLine();
Console.Write("enter password >> ");
string password = Console.ReadLine();
if (CheckUserPassword(accountsList, username, password))
{
List<Question> questions = FillQuestions();
int result = 0;
for (int i = 0; i < questions.Count; i++)
{
if (questions[i].DesiredGroup == accountsList.Find(x => x.UserName == username).Group)
{
continue;
}
Console.WriteLine();
Console.WriteLine(questions[i].QuestionText);
Console.WriteLine("--------------------------");
WriteAnswers(questions[i].AnswersList);
string answers = Console.ReadLine();
for (int j = 0; j < questions[i].AnswersList.Count; j++)
{
if (questions[i].AnswersList[j].AcceptableLetter == answers)
{
if (questions[i].AnswersList[j].IsCorrect)
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is correct");
result += 10;
}
else
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is incorrect");
}
}
}
}
if (result < 15)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
}
Console.Read();
}
private static void WriteAnswers(List<Answer> answersList)
{
char[] alphabetLetters = { 'a', 'b', 'c' };
for (int i = 0; i < answersList.Count; i++)
{
Console.WriteLine(alphabetLetters[i] + ") " + answersList[i].AnswerText);
}
}
private static List<Question> FillQuestions()
{
List<Question> questionList = new List<Question>();
List<Answer> answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "Ankara", IsCorrect = true, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Istambul", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Izmir", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Türkiyenin baskenti neresidir?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "ismet", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Atatürk", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Ali", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Baskomutan kim?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "2", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "4", IsCorrect = true, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "2 kere 2?", AnswersList = answerList, DesiredGroup = 2 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1912", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "1914", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "1915", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "When did the world war 1 start?", AnswersList = answerList, DesiredGroup = 2 });
return questionList;
}
private static bool CheckUserPassword(List<Account> accountsList, string username, string password)
{
foreach (Account account in accountsList)
{
if (account.UserName == username)
{
if (account.Password == password)
{
Console.WriteLine("welcome " + account.UserName);
return true;
}
else
{
Console.WriteLine("invalid access");
return false;
}
}
}
return false;
}
class Account
{
public string UserName { get; set; }
public string Password { get; set; }
public int Group { get; set; }
}
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
public int DesiredGroup { get; set; }
}
class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
总有更好的方法,但现在已足够:) 快乐的编码
答案 1 :(得分:0)
这里只是一小块。 @OrilesElkar的更长答案告诉您如何将其分解为更小的类是您需要的。
还有一个:
public class QuestionsByAccountProvider
{
Question[] GetQuestionsForAccount(Account account)
{
// Put your code for providing questions here.
}
}
然后从另一个班级你可以打电话
var questionProvider = new QuestionsByAccountProvider();
var questions = questionProvider.GetQuestions(account);
这有助于将代码的不同区域分开,这样当您查看程序时,您不会立即看到整个程序。即使它是您自己的代码,这也可能令人困惑。在任何特定时刻,您只需要查看您关心的逻辑部分。
另外,正如所指出的,也许以后你会想把你的问题放在数据库中。如果您的问题是由单独的课程提供的,那么您可以在那里进行更改并且更容易。如果它只是一个巨大的方法,那么在不破坏其他东西的情况下更改一个部分会变得更加困难。通过这种方式,您的主要方法只是知道它要求某些东西给它提问题,并且它并不关心这种情况如何发生。
或者当您开始工作时,您可能会决定将其放在Windows窗体应用程序或Web应用程序中。如果您的代码位于不同的类中,则可以将它们添加到其他类型的应用程序中,并且它们的工作方式相同。但是如果你的所有工作都在你的控制台应用程序的Main
中,那么提取一些部分并重用它们将非常困难。
当一个班级可以告诉他人做什么或从中获取信息而不知道其他班级如何开展工作时,它会很有帮助。这样您就可以对一个类进行更改而不会影响另一个类。你会发现你花更多的时间在&#34;有趣的&#34;编写代码的一部分 - 弄清楚如何使事情发生 - 并且更少的时间尝试阅读或调试它。
(下一步是unit testing,这真的很棒。我建议尽早了解它。我希望我有。)