如何循环?

问题描述:

我无法学习如何循环,我坚持如何做到这一点。基本上我被要求编程一个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

所有你需要做的是调用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(); 
     }