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:
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With