Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Operator `+' cannot be applied to operands of type `int' and `System.Random'

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
       }   
   } 
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading