Answer to Question #204262 in C# for Nkhululeko Chabala

Question #204262

Write a user defined method named getMark that can be used to get a valid mark from the user. Your method must continuously ask the user for a mark, until a valid mark is entered (valid marks are between 0 and 100). If a user enters an invalid mark, display an error message before asking for a valid mark to be entered. The method must return the valid value to the calling program.


1
Expert's answer
2021-06-07T17:20:51-0400

Solution example:

using System;

/*
 * Write a user defined method named getMark that can be used to get a valid mark from
 * the user. Your method must continuously ask the user for a mark, until a valid mark
 * is entered (valid marks are between 0 and 100). If a user enters an invalid mark,
 * display an error message before asking for a valid mark to be entered. The method
 * must return the valid value to the calling program.
 */
namespace getMark
{
    class Program
    {
        static void Main(string[] args)
        {
            int mark = getMark();
        }

        public static int getMark()
        {
            int mark;

            do
            {
                Console.WriteLine("Input a mark value between 0 and 100.");
                string input = Console.ReadLine();

                try
                {
                    mark = int.Parse(input);

                    if (mark >= 0 && mark <= 100)
                    {
                        return mark;
                    }
                    else
                    {
                        Console.WriteLine("Input an invalid mark value!");
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Input an invalid mark value!");
                }
            }
            while (true);
        }
    }
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS