using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ErrorHandling
{
class Program
{
static void Main(string[] args)
{
//input problem 1
//Declarations
byte value = 0;
bool tryagain = true;
//try loop while tryagain is true
while (tryagain)
{
Console.Write("Enter a Number Between 0 and 255: ");
try //try to Convert input
{
value = Convert.ToByte(Console.ReadLine());
tryagain = false; //if convert is successful: exit loop
}
catch (FormatException) //if input is not a number
{
value = 0; //set value to zero
Console.WriteLine("Error, Please Enter a Number!");
}
catch (OverflowException) //if number is too big
{
value = 0; //set value to zero
Console.WriteLine("Number was too Big! Enter between 0 and 255");
}
}
Console.WriteLine("Output Number is {0}", value); //output
Console.ReadKey();
Console.WriteLine(""); //Adds Space between problems
//input problem 2
//Declarations
string value2 = "";
bool valid = false;
//one way to do it
//opening statement
while (value2 != "rock" && value2 != "paper" && value2 != "scissors") //while input is not valid loop
{
Console.Write("Enter ‘rock’, ‘paper’, or ‘scissors’: ");
value2 = Console.ReadLine(); //input
value2 = value2.ToLower(); //converts input to all lowercase
}
Console.WriteLine("Input is Valid"); //input was valid, loop exits
//another way to do it
//if input is not valid, loop
while (!valid)
{
//opening statement
Console.Write("Enter ‘rock’, ‘paper’, or ‘scissors’: ");
value2 = Console.ReadLine(); //input
value2 = value2.ToLower(); //converts input to all lowercase
//if statements
if (value2 == "rock")
{
Console.WriteLine("Valid");
valid = true; //exit loop
}
else if (value2 == "paper")
{
Console.WriteLine("Valid");
valid = true; //exit loop
}
else if (value2 == "scissors")
{
Console.WriteLine("Valid");
valid = true; //exit loop
}
else
Console.WriteLine("Not Valid");
}
Console.ReadKey(); //Keeps window open for results
}
}
}
[/code]