program in C# for a console app

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myburt
    New Member
    • Jun 2007
    • 4

    #1

    program in C# for a console app

    Hi im writing a very "simple" program in C# for a console app..
    I'm supposed to make a program that asks a user how many dice to roll, rolls a six sided die that many times, and returns the total and average roll.(this is all coming out of a book "C# programming for absolute beginners")
    I cant figure how to get the value of the dice roll outside of the for statement please help.

    Code:
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random generator = new Random();
    
                int NumberOfRolls;
                
                Console.WriteLine("how many dice should i roll?");
                NumberOfRolls = int.Parse(Console.ReadLine());
    
                for (int i = 0; i < NumberOfRolls; i++) 
                {
                    double mydouble = (int)(generator.NextDouble() * 6) + 1;
                    int total = +(int)mydouble;
                    double average = total / NumberOfRolls;
                    Console.WriteLine(average);
                    
                }
                
            }
        }
    }
  • chinu
    New Member
    • Jun 2007
    • 36

    #2
    Originally posted by myburt
    Hi im writing a very "simple" program in C# for a console app..
    I'm supposed to make a program that asks a user how many dice to roll, rolls a six sided die that many times, and returns the total and average roll.(this is all coming out of a book "C# programming for absolute beginners")
    I cant figure how to get the value of the dice roll outside of the for statement please help.

    Code:
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random generator = new Random();
    
                int NumberOfRolls;
                
                Console.WriteLine("how many dice should i roll?");
                NumberOfRolls = int.Parse(Console.ReadLine());
    
                for (int i = 0; i < NumberOfRolls; i++) 
                {
                    double mydouble = (int)(generator.NextDouble() * 6) + 1;
                    int total = +(int)mydouble;
                    double average = total / NumberOfRolls;
                    Console.WriteLine(average);
                    
                }
                
            }
        }
    }
    What do you mean by "value of the dice roll". Do you mean "average"?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      When you decalare a varriable inside a set of { } brackets it is only available inside those (and nested) brackets. If you want the variable to exist outside the brackets, you must declare it outside the brackets
      Code:
      class Program
          {
              static void Main(string[] args)
              {
                  Random generator = new Random();
       
                  int NumberOfRolls;
                  
                  Console.WriteLine("how many dice should i roll?");
                  NumberOfRolls = int.Parse(Console.ReadLine());
                  int total=0;
                  for (int i = 0; i < NumberOfRolls; i++) 
                  {
                      double mydouble = (int)(generator.NextDouble() * 6) + 1;
                      total = +(int)mydouble;
                  }
                  double average = total / NumberOfRolls;
                  Console.WriteLine(average);
              }
          }

      Comment

      Working...