Jump to content

Repeating a method in C#

Ziggs

Hello!

 

So i'm writing a program that tests your math knowledge. I am a begginer in programming and i need help with a problem. How can i repeat a method 10 times. 

 

This is how my method that i want to repeat looks like:

 

        public static void Deljenje ()
        {
            Random numGen = new Random();
 
            int num01 = numGen.Next(1, 21);
            int num02 = numGen.Next(1, 11);
 
            Console.WriteLine(num01 + "/" + num02);
            int answer = Convert.ToInt32(Console.ReadLine());
            if (answer == num01 / num02)
            {
                Console.WriteLine("Bravo!");
            }
            else
            {
                int responseIndex = numGen.Next(1, 4);
 
                switch (responseIndex)
                {
                    default:
                        Console.WriteLine("To pa ni prav!");
                        break;
 
                    case 1:
                        Console.WriteLine("Bolj se potrudi!");
                        break;
 
                    case 2:
                        Console.WriteLine("Poskusi znova!");
                        break;
                }
            }
        }
 
Ps. just ignore the values in Console.Writeline - s. i'm slovenian
 
Thanks
Link to comment
Share on other sites

Link to post
Share on other sites

This forum really isnt the place to go for programming help. I would recommend Stack Overflow for help with C#.

CPU: Intel i5 4690k @ 4.3 GHz       GPU: MSI GTX 980                      Cooling: be quiet! Pure Rock                     OS: Windows 7            Monitor: BenQ XL2411Z

Motherboard: AsRock Z97 PRO4   PSU: Corsair 600W CX600M       Keyboard: Razer BlackWidow Ultimate   SSD: Intel 120GB 520   Headset: HyperX Cloud II

RAM: 12GB Crucial Ballistix            Case: Corsair 750D                       Mouse: Logitech G502                         HDD: Seagate 1TB        Speakers: Audioengine A5+

Link to comment
Share on other sites

Link to post
Share on other sites

for (int c=0;c<10;c++)

{

    //Your method

}

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

You could run a for loop to make your function run 10 times?
 

Example:

static void Main(string[] args){     // Repeats the function 10 times     for(int i = 0; i < 10; i++){          class_name.Deljenje();     }}

Hope this is what you are looking for.

Link to comment
Share on other sites

Link to post
Share on other sites

This forum really isnt the place to go for programming help. I would recommend Stack Overflow for help with C#.

 

@Ziggs ignore this comment. Stack Overflow is indeed a great place to ask and find answers to questions, however we're certainly here to help with your programming questions too.

Link to comment
Share on other sites

Link to post
Share on other sites

This forum really isnt the place to go for programming help. I would recommend Stack Overflow for help with C#.

 

Seconded please feel free to completely ignore the local idiot...

 

Please also take notice of the glaringly obvious announcement and use code tags when presenting your code. Just encase you missed that, it is quite distinctly located here:

 

PZ20Fqo.png

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Changes written in green, hope it helps

 

 public static void Deljenje ()
        {
            while(true)
            {


            Random numGen = new Random();


            int num01 = numGen.Next(1, 21);
            int num02 = numGen.Next(1, 11);


            Console.WriteLine(num01 + "/" + num02);
            int answer = Convert.ToInt32(Console.ReadLine());
            if (answer == num01 / num02)
            {
                Console.WriteLine("Bravo!");
            }
            else
            {
                int responseIndex = numGen.Next(1, 4);


                switch (responseIndex)
                {
                    default:
                        Console.WriteLine("To pa ni prav!");
                        break;


                    case 1:
                        Console.WriteLine("Bolj se potrudi!");
                        break;


                    case 2:
                        Console.WriteLine("Poskusi znova!");
                        break;
                }
            }
            Console.WriteLine("Do you want to continue?  N to stop/any other key to continue");
            string answer = Console.ReadKey().ToString();
            if(answer.ToLower()=="n")
            {

                break;

            }
            }

        }
 
 
Or if you want the call to be looped, just surround the function call with green lines written above (and remove from within the function of curse)

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

This forum really isnt the place to go for programming help. I would recommend Stack Overflow for help with C#.

Not true, while on stack overflow you will get more answers you will probably get them here too especially for simple questions like this.

 

Anyway you just need a for loop https://msdn.microsoft.com/en-us/library/ch45axte.aspx

Link to comment
Share on other sites

Link to post
Share on other sites

for (int c=0;c<10;c++)

{

//Your method

}

+1 this

Just put the for loop in your main method and call your new method from inside it.

"Her tsundere ratio is 8:2. So don't think you could see her dere side so easily."


Planing to make you debut here on the forums? Read Me First!


unofficial LTT Anime Club Heaven Society

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×