Skip to content

Instantly share code, notes, and snippets.

@janellbaxter
Last active February 19, 2017 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janellbaxter/bf72977d8c58cc3b588f06ed5aa8474b to your computer and use it in GitHub Desktop.
Save janellbaxter/bf72977d8c58cc3b588f06ed5aa8474b to your computer and use it in GitHub Desktop.
6.5 Return Values; Programming is Fun: C# Adventure Game (Learn C#)
/*
* 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