mardi 30 novembre 2021

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:

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



Aucun commentaire:

Enregistrer un commentaire