2016-09-28 51 views
0

我必須創建一個測驗,以隨機順序打印問題。我已經寫出了if語句中的所有問題,並且如果已經回答了10個問題,我希望所有問題都能夠以適當的答案顯示出來並與用戶輸入的答案進行比較。我還不知道如何做到這一點,所以代碼在1完成後進入下一個if語句。C#測驗隨機挑選問題和騎車直到完成

如何完成1如果語句(問題)和隨機數發生器指向另一個如果等等。

static void Main(string[] args) 
    { 
     string answer1; 
     string answer2; 
     string answer3; 
     string answer4; 
     string answer5; 
     string answer6; 
     string answer7; 
     string answer8; 
     string answer9; 
     string answer10; 
     int answeredQs = 0; 

     Random rnd = new Random(); 
     int questionNum = rnd.Next(1,10); 
     Console.WriteLine("Question Number: " + questionNum); 

      if (questionNum == 1) 
      { 
       Console.WriteLine("What is a CPU?"); 
       answer1 = Console.ReadLine(); 
       answeredQs = +1; 
      } 

      if (questionNum == 2) 
      { 
       Console.WriteLine("What does 'RAM' stand for?"); 
       answer2 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 3) 
      { 
       Console.WriteLine("What is RAM?"); 
       answer3 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 4) 
      { 
       Console.WriteLine("How do you measure how fast a processor is?"); 
       answer4 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 5) 
      { 
       Console.WriteLine("What is an ALU & what does it do?"); 
       answer5 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 6) 
      { 
       Console.WriteLine("What is a register?"); 
       answer6 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 7) 
      { 
       Console.WriteLine("What is EEPROM?"); 
       answer7 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 8) 
      { 
       Console.WriteLine("What is the difference between SRAM and DRAM?"); 
       answer8 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 9) 
      { 
       Console.WriteLine("What is ROM?"); 
       answer9 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (questionNum == 10) 
      { 
       Console.WriteLine("What does the Control Unit do?"); 
       answer10 = Console.ReadLine(); 
       answeredQs = answeredQs + 1; 
      } 

      if (answeredQs == 10) 
      { 
       Console.WriteLine("asdasdasd"); 
      } 
+0

太棒了!你的問題是什麼? – Fabjan

+0

好的,我已經做了調整;我如何完成1如果語句(問題)和隨機數發生器指向另一個如果等等。 –

+0

那麼,你可以把你所有的問題放在數組中,然後洗牌,然後逐個拿出來(當它們已經是隨機順序的時候)。你可以閱讀如何洗牌數組[這裏](http://stackoverflow.com/questions/4120002/how-to-randomize-array-items-and-then-crop-the-array-by-required-percent)。 – Fabjan

回答

2

你可以把你的if語句在一個循環(forwhile ...)

而且,你的代碼可能是通過把問題在例如一個ArrayList更具可讀性(或任何其他「數據存儲「結構,例如簡單的數組)。

然後,你可以玩這個結構中的問題的索引,總是循環。

希望這會有幫助

+0

好的,謝謝。我們的講師還沒有教過我們的陣列,所以不知道。 –

+1

好吧,我假設你已經看到了循環呢?這可能是你正在尋找的 –

+0

雖然我們已經做了一些,但For循環沒有。所以你說的是我使用循環我不得不把所有的東西放入數組和循環內的數組? –