Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a c# function to compare int results

Tags:

function

c#

This will be so easy for some of you programming geniuses out there but I am a student who recently began learning about C# (and programming in general) and I find myself.... stuck.

This is an assessment I am working on so I am not looking for a copy/paste answer, it would be preferable if I could find out where I am going wrong/where to start so I can fix it myself.

The aim of the assessment is to:

  1. use the random number generator to generate 4 numbers - 2 for player 1 and 2 for the dealer.
  2. Add players 2 numbers together, add dealers 2 numbers together (show results on screen)

    this is where I become stuck...

  3. I need to create a function that basically says:

    • If DealerResult is > PlayerResult display: Dealer Wins.
    • If PlayerResult > DealerResult, display: You win.
    • If DealerResult == PlayerResult display: it is a draw.

So far I have the following code. As you will see, I can generate the numbers, add them together and display the results on screen.

using System;

namespace Assessment
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //Decalre Variables
            Random r = new Random ();
            int PlayerNumber1 = r.Next (6, 25);
            int PlayerNumber2 = r.Next (6, 25);
            int DealerNumber1 = r.Next (6, 25);
            int DealerNumber2 = r.Next (6, 25);
            int PlayerTotal = (PlayerNumber1 + PlayerNumber2);
            int DealerTotal = (DealerNumber1 + DealerNumber2);


            Console.WriteLine ("Welcome!");
            Console.ReadLine ();
            Console.WriteLine ("Your total is: " + PlayerTotal);
            Console.ReadLine ();
            Console.WriteLine ("Dealer total is: " + DealerTotal);
            Console.ReadLine ();


        }

    }
}

From here, I am stuck. Suggestions would be so appreciated as to how I should proceed to compare the numbers and display the appropriate result/s through a function.

As mentioned earlier, this is an assessment so I am not looking for a quick fix or final answer. Also, the assessment requires the use of a FUNCTION to generate the result, not a loop or any other type of programming magic that some of you super-geniuses may be aware of. (And I say that with envy - I wish I was half as smart as some of the people I see posting on here). :)

like image 959
Nicole Haines Avatar asked Mar 08 '14 00:03

Nicole Haines


1 Answers

You just need simple if statements, and put them into a function:

private static void DisplayResult(int playerTotal, int dealerTotal)
{
   if(playerTotal > dealerTotal)
   {
       Console.WriteLine("You win!");
   }
   else if(playerTotal < dealerTotal)
   {
       Console.WriteLine("Dealer wins!");
   }
   else
   {
      Console.WriteLine("Draw!");
   } 
}

Explanation: We create a function that takes two int parameter.One of them is playerTotal, another is dealerTotal.The function compare these values and display the proper result in the console according to this comparison.After you create your function all you need to do is pass PlayerTotal and DealerTotal variables to your function like this:

 DisplayResult(PlayerTotal, DealerTotal);

Note: You should put this method into MainClass

like image 58
Selman Genç Avatar answered Sep 18 '22 20:09

Selman Genç