2017-05-27 59 views
0

我試圖在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

如果你有*幾個*用戶,我建議把它們以及qustions和答案到*數據庫* –

+0

是的,你是對的,但因爲我正在探索2d陣列,我只定義了2個不同的用戶,並旨在使用數組來做 – vinjakci

+0

你有沒有嘗試過任何東西?你的問題太廣泛了。顯而易見的答案是「找出你正在處理的用戶,然後只詢問你想問他們的問題」。但有很多不同的方法可以做到這一點。如果你的代碼顯示了這樣做是不行的,你需要準確解釋你遇到的問題。代碼是做什麼的,你想要什麼,以及具體哪些_specifically_你無法工作?如果你的代碼是「之前」的版本,並沒有試圖解決問題,那麼你還沒有準備好提出一個問題。 –

回答

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; } 
    } 

而且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; } 
    } 

總是有更好的方法,但這足以讓現在:) 快樂編碼

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中,那麼提取某些部分並重新使用它們將非常困難。

當一個班級能夠告訴其他人如何處理或從中獲取信息而不知道其他班級的工作情況時,這很有幫助。這樣,您可以對一個類進行更改,而不會影響其他類。你會發現你花更多的時間來處理編寫代碼的「有趣」部分 - 搞清楚如何讓事情發生 - 並且減少嘗試讀取或調試它的時間。

(下一個步驟是unit testing,這實在是真棒。我建議及早了解它。我希望我有。)

相關問題