Skip to content

Instantly share code, notes, and snippets.

@AFelipeTrujillo
Created July 15, 2015 06:26
Show Gist options
  • Save AFelipeTrujillo/56b851916725b330a61e to your computer and use it in GitHub Desktop.
Save AFelipeTrujillo/56b851916725b330a61e to your computer and use it in GitHub Desktop.
The if Decision Statement and the Conditional Operator with C#
using System;
namespace CSharpBeginner
{
public class ConditionalStatementClass
{
public ConditionalStatementClass ()
{
}
public void readFromConsole()
{
Console.WriteLine ("what is your name?");
string name = Console.ReadLine ();
Console.WriteLine ("Your Name is: " + name);
}
public void getAPrize()
{
//Read from console
Console.WriteLine ("Which number? 1,2,3");
//use try in order to get any error
try{
//Convert a string to a number
int number = int.Parse(Console.ReadLine ());
// Get a decision
if(number == 1){
Console.WriteLine("you won a car !!");
}else if(number == 2){
Console.WriteLine("you won a dinner");
}else if(number == 3){
Console.WriteLine("you won a lovely cat :D");
}else{
Console.WriteLine("you did not won :( ");
}
}catch{
Console.WriteLine ("ERROR");
}
}
public void getAPrizeShort(){
//Read from console
Console.WriteLine ("Which number? 1,2,3");
try{
//Convert a string to a number
int number = int.Parse(Console.ReadLine ());
string message = (number == 1) ? "car" : "nothing !!";
Console.WriteLine("You won a {0}", message);
}catch{
Console.WriteLine ("ERROR");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment