Home > Enterprise >  Operator ` ' cannot be applied to operands of type `int' and `System.Random'
Operator ` ' cannot be applied to operands of type `int' and `System.Random'

Time:12-01

i'm a beginner to c#,so i was trying to make a program that rolls a die for you and the enemy for 10 turns,each turns adding the number of your roll to an overall count and whoever got the largest in the end won,i didn't finish it all the way but this is what i have so far:

namespace dfgs
{
   class dice
   {
       public static void Main(String[] args)
       {
           int plsc = 0;
           int aisc = 0;
           int turns = 0;
           Random plrnd = new Random();
           Random airnd = new Random();
           while (turns < 11)
            {
                Console.WriteLine("Player Roll Is"   Convert.ToInt32(plrnd.Next(6)));
                Console.WriteLine("AI Roll Is"   Convert.ToInt32(airnd.Next(6)));
                plsc = plsc   plrnd;
                aisc = aisc   airnd;
                Console.WriteLine("Type A and hit enter to go again");
                string nxt = Console.ReadLine();
                if (nxt == "A"){
                    turns  ;
                }
                Console.ReadLine();
            }
            
       }   
   } 
}

and whenever i try to compile i get the error Operator ' cannot be applied to operands of type int' and System.Random',this error appears twice,i tried changing the type of the random numbers to int but then i got the error messege Type int' does not contain a definition for Next' and no extension method Next' of type int' could be found. Are you missing an assembly reference? i am kinda stuck here,any help would be appreciated.

EDIT:Thank you to everyone who answered, i have managed to make this work,here is the final code,it's not the cleanest but works as intended:

namespace dfgs
{
   class die
   {
       public static void Main(String[] args)
       {
           int plsc = 0;
           int aisc = 0;
           int turns = 0;
           Random plrnd = new Random();
           Random airnd = new Random();
           while (turns < 10)
            {
                Console.WriteLine("Player Roll Is "   Convert.ToInt32(plrnd.Next(6)));
                Console.WriteLine("AI Roll Is "   Convert.ToInt32(airnd.Next(6)));
                plsc = plsc   plrnd.Next(6);
                aisc = aisc   airnd.Next(6);
                Console.WriteLine("Type A and hit enter to go again");
                string nxt = Console.ReadLine();
                if (nxt == "A"){
                    turns  ;
                }
                else{
                    break;
                }
                if (turns == 10){
                    if (plsc > aisc){
                        Console.WriteLine("The Player Has Won,Ai Score: "   aisc   " Player Score: "   plsc);
                    }

                    else if (aisc > plsc){
                        Console.WriteLine("The AI Has Won,Ai Score: "   aisc   " Player Score: "   plsc);
                    }
                    break;
                    
                }
               
            }
            Console.ReadLine();
       }   
   } 
}

CodePudding user response:

You probably want to save the random int you get from the Random, rather than trying to calculate number, plus a random_number_generator_machine - which, in some real world terms would be like asking someone "what is your age, divided by a cheese sandwich?"..

            var plroll = plrnd.Next(6);
            var airoll = airnd.Next(6);
            Console.WriteLine("Player Roll Is"   plroll);
            Console.WriteLine("AI Roll Is"   airoll));
            plsc = plsc   plroll;
            aisc = aisc   airoll;

You don't need a Random for the player and a Random for the computer; one Random could adequately generate for both, btw:

       int plsc = 0;
       int aisc = 0;
       int turns = 0;
       Random r = new Random();
       while (turns < 11)
       {
            var plroll = r.Next(6);
            var airoll = r.Next(6);
            Console.WriteLine("Player Roll Is"   plroll);
            Console.WriteLine("AI Roll Is"   airoll));
            plsc = plsc   plroll;
            aisc = aisc   airoll;

CodePudding user response:

Look at this line:

plsc = plsc   plrnd;

In this code, plrnd is not a number. Instead, it's a generator for producing numbers. You must tell the generator to give you the next number, as you did on the line above:

plrnd.Next(6)

Moreover, you likely need to change the code above to save the results of each call in variables so you can use that same value to both show to the user and increment the total count.

CodePudding user response:

Since you want to use the result of the roll multiple times, start by assigning the results to a variable for later use:

while (turns < 11)
{
    // roll dies, store results in local variable
    var plRoll = plrnd.Next(6);
    var aiRoll = airnd.Next(6);

    // then reuse the variables
    Console.WriteLine("Player Roll Is "   plRoll);
    Console.WriteLine("AI Roll Is "   aiRoll);
    plsc = plsc   plRoll;
    aisc = aisc   aiRoll;
    Console.WriteLine("Type A and hit enter to go again");
    string nxt = Console.ReadLine();
    if (nxt == "A"){
        turns  ;
    }
    else {
        // remember to break out if the player doesn't want to continue
        break;
    }
}

CodePudding user response:

int is a built in value data type in C#. Random is a class object. The operator does not know how to add an int data type and a Random class object together. What you probably meant to do is to use the Next() method, which does return an int. Also, you only want to new up the Random class one time. Consider using this code instead:

namespace dfgs
{
   class dice
   {
       public static void Main(String[] args)
       {
           Random rnd = new Random();
           int plsc = 0;
           int aisc = 0;
           int turns = 0;
           int plrnd = rnd.Next(6);
           int airnd = rnd.Next(6);
          while (turns < 11)
            {
                Console.WriteLine("Player Roll Is"   plrnd);
                Console.WriteLine("AI Roll Is"   airnd);
                plsc = plsc   plrnd;
                aisc = aisc   airnd;
                Console.WriteLine("Type A and hit enter to go again");
                string nxt = Console.ReadLine();
                if (nxt == "A"){
                    turns  ;
                }
                Console.ReadLine();
            }
        
       }   
   } 
}
  • Related