首先讓我告訴你完全錯誤的做法。這是最髒,最快捷的修復,那我可以這樣做:
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; }
}
而且finnaly甲A nswer問題,以幫助我們有點邏輯:
class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
填充這些類是容易的,它幾乎比你的例子一樣,但更精緻的名字。名單是你的朋友在這裏,您可以將所有帳戶存儲在現場,但沒有那些討厭的指標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;
}
而且使用此框架,你可以簡單地實現它的問題是其通過增加物業的譯文類:
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
// added property
public int DesiredGroup { get; set; }
}
和分配哪些問題是什麼組FillQuestions()方法: // code ... questionList.Add(新問題{QuestionText =「什麼時候第一次世界大戰開始?」,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; }
}
總是有更好的方法,但這足以讓現在:) 快樂編碼
如果你有*幾個*用戶,我建議把它們以及qustions和答案到*數據庫* –
是的,你是對的,但因爲我正在探索2d陣列,我只定義了2個不同的用戶,並旨在使用數組來做 – vinjakci
你有沒有嘗試過任何東西?你的問題太廣泛了。顯而易見的答案是「找出你正在處理的用戶,然後只詢問你想問他們的問題」。但有很多不同的方法可以做到這一點。如果你的代碼顯示了這樣做是不行的,你需要準確解釋你遇到的問題。代碼是做什麼的,你想要什麼,以及具體哪些_specifically_你無法工作?如果你的代碼是「之前」的版本,並沒有試圖解決問題,那麼你還沒有準備好提出一個問題。 –