Creating a c# function to compare int results

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:

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

    this is where I become stuck...

  • 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). :)


    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


    To start you will need a function. Functions look like this:

    <access modifier> <return type> <Name> ( <parameters> )    
    {}
    

    A quick example:

    private bool GetResult (int playerValue, int dealerValue)
    {
    }
    

    What this means is that the function will return a bool, and it takes two int parameters. To return nothing, return void. To call the function, use its name and pass the parameters inside the parenthesis:

    bool result = GetResult(1, 2);
    

    Now to do a comparison, we use the if statement:

    if (<expression> <comparator> <expression>)   
    {}
    

    Another quick example:

    if (playerScore > dealerScore)
    {
       Console.WriteLine("Player wins!");
    }
    

    Which says, "If PlayerScore is greater than DealerScore, do what is inside the brace" (a print in this case).

    I'm trying to explain the basics, instead of give an actual answer, as you requested. Please let me know if I can clarify anything better, and good luck learning programming in C#!


    Another variation demonstrating function returning result as a string.

     
    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();
            Console.WriteLine("Dealer total is: " + DealerTotal);
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(DisplayResult(PlayerTotal, DealerTotal));
    
            Console.ReadLine();
    
    
    
        }
    
        private static string  DisplayResult(int playerTotal, int dealerTotal)
        {
            var result = "An unhandled exception has occured ";
    
            if (playerTotal > dealerTotal)
            {
                result = "You win!";
            }
            else if (playerTotal < dealerTotal)
            {
                result = "Dealer wins!";
            }
            else
            {
                result = "Draw!";
            }
    
            return result; 
        }
    
    }
    

    }

    链接地址: http://www.djcxy.com/p/77860.html

    上一篇: C#

    下一篇: 创建ac#函数来比较int结果