2
Answers

Have problem generating a random number using a Method

Ask a question
Terry Bockman

Terry Bockman

11y
2.1k
1

  I am using C# 2010, Express Edition on Windows 7 Pro.  I am teaching myself C#, and have a problem with a program.

  I can generate a random integer using just the Main Method.  But, when I try to call a random number from a method, the compiler generates errors.  I have written (simple) methods before without any problem.  But, I am stuck on this one.  I would appreciate any help!!!

  Below is the source code and compiler errors.  Thanks, Terry

---------------------------------------------------------------------
// Lines 1-5 are comments only.
using System;
public class RandomInt
{
// L9
   public static void Main()
   {
// L12
     
      int d0 = 0;

      // call GetRandomNumber Method
      d0 = GetRandomNumber(int number);

      Console.Out.WriteLine("d0 = " + d0);

// L21


   } // end Main
} // end class RandomInt

// L27
      // begin Method GetRandomNumber
      public static int GetRandomNumber(int MethodNumber)
      {
         Random random = new Random();
         int randomNumber = random.Next(1,7);
         MethodNumber = randomNumber;
         return MethodNumber;
      } // end Method GetRandomNumber


--------- COMPILER ERRORS ----------

Score3b.cs(19,12): error CS1513: } expected
Score3b.cs(21,28): error CS1519: Invalid token '(' in class, struct, or
        interface member declaration
Score3b.cs(21,41): error CS1519: Invalid token ')' in class, struct, or
        interface member declaration
Score3b.cs(27,1): error CS1022: Type or namespace definition, or end-of-file
        expected
Score3b.cs(31,21): error CS1518: Expected class, delegate, enum, interface, or
        struct
Score3b.cs(33,30): error CS1518: Expected class, delegate, enum, interface, or
        struct
Score3b.cs(37,7): error CS1022: Type or namespace definition, or end-of-file
        expected
v


 


Answers (2)