2014-10-27 42 views
0

我無法學習如何循環,我堅持如何做到這一點。基本上我被要求編程一個6面的骰子,它會問你要擲幾次。根據你滾動的次數,它會列出它在每一側落地多少次的表格。這是我迄今爲止所擁有的。如何循環?

using System; 

namespace Dice 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      bool continueRunning = true; 
      int sessionNumber = 1; 

      DisplayInstructions(); 

      while (continueRunning) 
      { 
       int howMany = int.Parse(getInfo("How many times do you want to roll the die?")); 
       Dice aDice = new Dice(); 
       aDice.RollDice(); 

       Console.Clear(); 
       Console.WriteLine("Session Number: {0}", sessionNumber); 
       Console.WriteLine(aDice); 

       continueRunning = getYorN("Would you like to run again?"); 
       sessionNumber++; 
       Console.Clear(); 
      } 
     } 

     public static bool getYorN(string question) 
     { 
      bool validInput = false;    

      while (!validInput) 
      { 
       Console.WriteLine("{0}", question); 
       Console.WriteLine("Enter 'yes' or 'no' to continue..."); 
       string userResponse = Console.ReadLine().ToLower(); 
       if (userResponse == "yes" || userResponse == "no") 
       { 
        validInput = true; 
        switch (userResponse) 
        { 
         case "yes": 
          return true; 

         case "no": 
          return false; 

         default: 
          return false; 
        } 

       } 
       else 
       { 
        Console.Clear(); 
        Console.WriteLine("You've entered an invalid term"); 
       } 
      } 

      return false; 
     } 

     public static void DisplayInstructions() 
     { 
      Console.WriteLine("Welcome to the Dice Game!!"); 
      Console.WriteLine("\n\n\nThis program will simulate rolling a die and will track the frequency \neach value is rolled."); 
      Console.WriteLine("\n\n\nAfter rolling the die, the program will output a summary table for the session."); 
      Console.WriteLine("\n\n\nPlease press any key to continue."); 
      Console.ReadKey(); 
      Console.Clear(); 
     } 

     public static string getInfo(string what) 
     { 
      Console.WriteLine(what); 
      return Console.ReadLine(); 
     } 
    } 
} 

我在這個類是

using System; 

namespace TripCalcApp 
{ 
    class Dice 
    { 
     private int side1 = 0, side2 = 0, side3 = 0, side4 = 0, side5 = 0, side6 = 0; 

     Random randNum = new Random(); 

     public Dice() 
     { 
     } 

     public int Side1 
     { 
      get { return side1; } 
      set { side1 = value; } 
     } 
     public int Side2 
     { 
      get { return side2; } 
      set { side2 = value; } 
     } 
     public int Side3 
     { 
      get { return side3; } 
      set { side3 = value; } 
     } 
     public int Side4 
     { 
      get { return side4; } 
      set { side4 = value; } 
     } 
     public int Side5 
     { 
      get { return side5; } 
      set { side5 = value; } 

     } 
     public int Side6 
     { 
      get { return side6; } 
      set { side6 = value; } 
     } 

     public void RollDice() 
     //RollDice = randNum.Next(1, 7) 
     { 

      switch (randNum.Next(1, 7)) 
      { 
       case 1: side1++; 
        break; 
       case 2: side2++; 
        break; 
       case 3: side3++; 
        break; 
       case 4: side4++; 
        break; 
       case 5: side5++; 
        break; 
       case 6: side6++; 
        break; 
      } 
     } 

     public override string ToString() 
     { 
      return "       Freq. Rolls           " + 
       "________________________________________" + 
       "\nSide 1 of Die rolled  :" + side1 + 
       "\nSide 2 of Die rolled  :" + side2 + 
       "\nSide 3 of Die rolled  :" + side3 + 
       "\nSide 4 of Die rolled  :" + side4 + 
       "\nSide 5 of Die rolled  :" + side5 + 
       "\nSide 6 of Die rolled  :" + side6 + 
       "\n"; 
     } 
    } 
} 

我對如何做循環,但林仍不能確定的想法。我想到了這樣的事情,但它不起作用,我希望你們能幫助我!

 int howMany = int.Parse(getInfo("How many times would you like to roll the die?")); 
     do 
     { 
      Dice aDice = new Dice(); 

      for (int counter = howMany; counter > 0; counter--) 
      { 
       aDice.RollDice(); 
      } 

      while (howMany < 0) 
      { 
       Console.WriteLine(aDice); 
      } 
      Console.Clear(); 
      Console.WriteLine("Session Number: {0}", sessionNumber); 
      Console.WriteLine(aDice); 
      playAgain = getYorN("Would you like to play again?"); 

      sessionNumber++; 
     } 
+0

請注意:反勾'是內聯格式。對於塊代碼格式,縮進4個空格。 – crashmstr 2014-10-27 12:00:24

+2

您一直在創建新的骰子,併爲每個骰子分配一個新的隨機數。把它作爲一個隨機的,而不是隨機的數字,因爲所有的都是用與種子相同的時間戳創建的!除此之外:__什麼「不工作」以及如何? – TaW 2014-10-27 12:05:27

+0

用一個由6個元素組成的陣列替換6個成員'side1'''side6',並調整它的代碼。而不是大開關,你會只有一行,就像這個'allSides [randNum.Next(0,6)] ++;' – Dialecticus 2014-10-27 12:07:28

回答

2

所有你需要做的是調用aDice.RollDice方法的howmany時間:

 while (continueRunning) 
     { 
      int howMany = int.Parse(getInfo("How many times do you want to roll the die?")); 
      Dice aDice = new Dice(); 
      for(int i = 0; i < howMany; i++) 
      { 
       aDice.RollDice(); 
      } 

      Console.Clear(); 
      Console.WriteLine("Session Number: {0}", sessionNumber); 
      Console.WriteLine(aDice); 

      continueRunning = getYorN("Would you like to run again?"); 
      sessionNumber++; 
      Console.Clear(); 
     }