Creating a C # function to compare int results

It will be so easy for some of you brilliant programmers, but I'm a student who recently started to learn C # (and generally programming), and I found that ... stuck.

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

The purpose of the assessment is as follows:

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

    that's where i get stuck ...

  • I need to create a function that basically says:

    • If DealerResult is> PlayerResult display: Dealer Wins.
    • If PlayerResult> DealerResult, display: you won.
    • If DealerResult == Display PlayerResult: this is a draw.

So far I have the following code. As you will see, I can generate numbers, add them together and display the results on the 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 (); } } } 

I'm stuck from here. The suggestions will be so appreciated, how should I go to the comparison of numbers and display the corresponding result / s through the function.

As mentioned earlier, this is an estimate, so I am not looking for a quick fix or a final answer. In addition, evaluation requires the use of the FUNCTION function to generate a result, not a loop or any other type of programming magic that some of you gnomes may know about. (And I say this with envy - I wish I were as smart as some of the people I see here). :)

+7
function c #
source share
3 answers

You just need simple if statements and put them in 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 are creating a function that takes two int parameters. One of them is playerTotal , the other is dealerTotal . The function compares these values ​​and displays the correct result in the console in accordance with this comparison. After you create your function, you just need to pass the playerTotal and dealerTotal as follows:

  DisplayResult(PlayerTotal, DealerTotal); 

Note. You must put this method in MainClass

+2
source share

You need a function to run. The functions are as follows:

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

Quick example:

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

This means that the function will return bool and require two int parameters. To return nothing, return the void. To call a function, use its name and pass the parameters in parentheses:

 bool result = GetResult(1, 2); 

Now, to perform the comparison, we use the if statement:

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

Another quick example:

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

That says: “If PlayerScore is larger than DealerScore, do what is inside the bracket” (print in this case).

I am trying to explain the basics, instead of giving a real answer, as you requested. Please let me know if I can clarify anything better, and good luck with C # programming!

+2
source share

Another option that demonstrates a function that returns the 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; } } 

}

0
source share

All Articles