107 308
Assignments Done
98.9%
Successfully Done
In March 2023

# Answer to Question #180527 in C# for Miley

Question #180527

you are creating a small game for school kids. Game has multiple steps:

1Display following message to user: “Enter any number from 1-5"

2User enters an option from 1-5, show the exact message to user for the number selected

1Enter even number

2Enter odd number

3Enter a prime number

4Enter a negative number

5Enter zero

for e.g. if user has selected 1, then show “Enter even number”,

If user does not enter correct number from 1-5 show error message. and then -> GOTO step 1

3.If user has entered correct number, then show success, else show error. -> after this GOTO step 1

To validate type of user input, there should be a validation method, which will return true if user input is correct, else validation method will throw different exception for different failure scenario.

Create a CustomException class.

Also, when user has played this game for 5 times, show a message to user you have played this game for 5 times. Handle this also using exception.

1
2021-04-17T07:54:19-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Q180527
{
class InvalidNumberException : Exception{
public InvalidNumberException(string message)

: base(message){}
}

class Program{
static void Main(string[] args){

//1 Display following message to user: “Enter any number from 1-5"
//2 User enters an option from 1-5, show the exact message to user for the number selected
int number = -1;

int timesPlayed = 0;
//you have played this game for 5 times.
while (timesPlayed < 5)
{
try
{
Console.Write("Enter any number from 1-5: ");
//If user does not enter correct number from 1-5 show error message. and then -> GOTO step 1
if (!userInputIsCorrect(inputString, 1, 5, 0, false))
{
number = -1;
}
else
{
int.TryParse(inputString, out number);
timesPlayed++;
if (number == 1)
{
}
else if (number == 2)
{

}
else if (number == 3)
{
getAnswer("Enter a prime number: ", number);

}
else if (number == 4)
{
getAnswer("Enter a negative number: ", number);

}
else if (number == 5)
{
getAnswer("Enter a zero number: ", number);
}
}

}
catch (InvalidNumberException ex)
{
Console.WriteLine(ex.Message);
number = -1;
}
}
Console.WriteLine("\nYou have played this game for 5 times.\n");

}
/// <summary>
/// </summary>
/// <param name="message"></param>
/// <param name="method"></param>
static void getAnswer(string message, int method)
{
const int minValue = -1000000;
const int maxValue = 1000000;
Console.Write(message);
if (userInputIsCorrect(inputString, minValue, maxValue, 0, false))
{
if (userInputIsCorrect(inputString, minValue, maxValue, method, true))
{
}
}
}

/// <summary>
/// Check if number is prime
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
static bool isPrime(int number)
{
if (number == 1) return false;
if (number == 2) return true;

for (int i = 2; i <= Math.Ceiling(Math.Sqrt(number)); ++i)
if (number % i == 0)
return false;
return true;
}
/// <summary>
/// Checks if user input is correct
/// </summary>
/// <param name="inputString"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="method"></param>
/// <returns></returns>
static bool userInputIsCorrect(string inputString, int min, int max, int method, bool isNextFunctions)
{

if (!isNextFunctions)
{
int number = -1;
if (!int.TryParse(inputString, out number))
{
throw new InvalidNumberException(string.Format("Error: enter any integer number from {0}-{1}.", min, max));
}
else
{
if (number < min || number > max)
{
throw new InvalidNumberException(string.Format("Error: enter any number from {0}-{1}.", min, max));
}
return true;
}
}
else
{
int number = -1;
int.TryParse(inputString, out number);
if (method == 1)
{
if (number % 2 == 0)
{
return true;
}
else
{
throw new InvalidNumberException("\nIt is not even number. Try again.\n");
}

}
if (method == 2)
{
if (number % 2 == 1)
{
return true;
}
else
{
throw new InvalidNumberException("\nIt is not odd number. Try again.\n");
}

}
if (method == 3)
{
if (isPrime(number))
{
return true;
}
else
{
throw new InvalidNumberException("\nIt is not a prime number. Try again.\n");
}

}
if (method == 4)
{
if (number < 0)
{
return true;
}
else
{
throw new InvalidNumberException("\nIt is not a negative number. Try again.\n");
}

}
if (method == 5)
{
if (number == 0)
{
return true;
}
else
{
throw new InvalidNumberException("\nIt is not a zero number. Try again.\n");
}

}
return false;
}
}
}
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!