Last active
February 19, 2017 23:28
-
-
Save janellbaxter/bf72977d8c58cc3b588f06ed5aa8474b to your computer and use it in GitHub Desktop.
6.5 Return Values; Programming is Fun: C# Adventure Game (Learn C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 6.5 Return Values | |
* Example code from Programming is Fun: C# Adventure Game | |
* Learn C# (for beginners) | |
* http://programmingisfun.com/learn/c-sharp-adventure-game | |
*/ | |
using System; | |
namespace ReturnValues | |
{ | |
class Character | |
{ | |
static int luck = 5; | |
static Random randomNumber = new Random(); | |
public static int points = 0; | |
public static bool Luck() | |
{ | |
int karma = randomNumber.Next(10); | |
bool result = false; | |
if (luck >= karma) | |
{ result = true; } | |
return result; | |
} | |
public static int Add(int num1, int num2) | |
{ | |
return num1 + num2; | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
Console.Title = "Return Value Examples"; | |
Console.WriteLine("------------------------------------"); | |
Console.WriteLine("Example: Boolean return value"); | |
Console.WriteLine("------------------------------------"); | |
Console.WriteLine("Your starting luck: " + Character.points); | |
for (int i = 0; i < 20; i++) | |
{ | |
if (Character.Luck()) | |
{ | |
Console.WriteLine("Your luck has gained you another point!"); | |
Character.points++; | |
} | |
else | |
{ | |
Console.WriteLine("Your luck has failed.... you've lost a point."); | |
Character.points--; | |
} | |
} | |
Console.WriteLine("Current luck: " + Character.points); | |
Console.WriteLine("\n\n------------------------------------"); | |
Console.WriteLine("Example: Integer return value"); | |
Console.WriteLine("------------------------------------"); | |
Console.Write("Enter first number: "); | |
int num1 = Convert.ToInt32(Console.ReadLine()); | |
Console.Write("Enter second number: "); | |
int num2 = Convert.ToInt32(Console.ReadLine()); | |
Console.Write(num1 + " plus " + num2 + " is " + Character.Add(num1,num2)); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment