简体   繁体   中英

C# coding input output issue

C# coding.

So I need help changing a line of code. I have put ******** and bolded the section I need help with. When you run the code and get to this part of the input I would like the output to display what you input exactly. What I mean is if you input a lowercase "i" it needs to output "i is not valid code", it needs to be lowercase too in the output. But if you were to input a capital "L" it needs to output "L is not a valid code" with a capital letter output. If this doesn't make sense please let me know. I am having trouble figuring out the right way to tell my code to do that. And I tried to Google an answer but I don't know how to word what I want to ask in a sort searchable way. Of course, take out the stars to run the code.

My code:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int contCurr;
            int contLast;
            Console.WriteLine("Please enter the number of contestants from last years contest");
            contLast = cont();
            Console.WriteLine("\nPlease enter the number of contestants from this years contest");
            contCurr = cont();
            array(contCurr);
            comp(contCurr, contLast);
        }

        public static int cont()
        {
            int last = 0;
            string b;
            b = (Console.ReadLine());
            int.TryParse(b, out last);
            while (last > 30 || last < 0)
            {
                Console.WriteLine("\nYou have entered and invalid response, please enter a valid number between 0 and 30.");
                b = (Console.ReadLine());
                int.TryParse(b, out last);
            }
            return last;
        }

        public static void array(int thisyear)
        {
            string[] contestant = new string[thisyear];
            string[] skill = new string[thisyear];
            for (int x = 0; x < thisyear; ++x)
            {
                Console.WriteLine("\nPlease enter contestant " + (x + 1) + "'s name");
                contestant[x] = Console.ReadLine();
                bool correct = false;
                while (!correct)
                {
                    Console.WriteLine("\nPlease enter contestant " + (x + 1) + " 's skill, 'S' for sing 'D' for dance 'M' for " +
                    "musical instrument 'O' for other");
                    string type = Console.ReadLine().ToUpper();

                    if (type == "S" || type == "D" || type == "M" || type == "O")
                    {
                        skill[x] = type;
                        correct = true;
                    }
                    else
                    { Console.WriteLine("\n{0} is not valid code",type); }
                }
            }

            talent(skill, contestant);
        }

        public static void talent(string[] skill, string[] contestant)
        {
            int dance = 0;
            int instrument = 0;
            int sing = 0;
            int other = 0;
            string entry;

            for (int x = 0; x < skill.Length; ++x)
            {
                if (skill[x] == "O")
                { ++other; }
                else if (skill[x] == "S")
                { ++sing; }
                else if (skill[x] == "D")
                { ++dance; }
                else if (skill[x] == "M")
                { ++instrument; }
            }

            Console.Clear();
            Console.WriteLine("The types of talent are:");
            Console.WriteLine("Singing {0}",sing);
            Console.WriteLine("Dancing {0}", dance);
            Console.WriteLine("Musical instrument {0}", instrument);
            Console.WriteLine("Other {0}", other);
            Console.WriteLine("\nPlease enter a skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or enter 'Z' to exit");

            entry = Console.ReadLine().ToUpper();
            while (entry != "Z")
            {
                if (entry != "S" && entry != "D" && entry != "M" && entry != "O")
                {
                    **Console.WriteLine("\n{0} is not a valid code.", entry);*********
                    Console.WriteLine("\nPlease try again: Enter a VALID skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or 'Z' to exit");

                    entry = Console.ReadLine().ToUpper();
                    if (entry == "Z")
                        break;
                }

                for (int x = 0; x < skill.Length; ++x)
                {
                    if (entry == skill[x])
                    {
                        if (entry == "S")
                        {
                            Console.WriteLine("Contestants with talent Singing are: ");
                            Console.WriteLine(contestant[x]);
                        }
                        else if (entry == "M")
                        {
                            Console.WriteLine("Contestants with talent Musical instrument are: ");
                            Console.WriteLine(contestant[x]);
                        }
                        else if (entry == "D")
                        {
                            Console.WriteLine("Contestants with talent Dancing are: ");
                            Console.WriteLine(contestant[x]);
                        }
                        else
                        {
                            Console.WriteLine("Contestants with talent Other are: ");
                            Console.WriteLine(contestant[x]);
                        }
                    }
                }

                Console.WriteLine("\nPlease enter a skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or enter 'Z' to exit");

                entry = Console.ReadLine().ToUpper();
            }
        }

        public static void comp(int contCurr, int contLast)
        {
            if (contCurr > contLast * 2)
            {
                Console.WriteLine("\nThe competition is more than twice as big this year!\n");
                Console.WriteLine("\nThe revenue expected for this year's competition is {0:C}", (contCurr * 25));
            }
            else
                 if (contCurr > contLast && contCurr <= (contLast * 2))
            {

                Console.WriteLine("\nThe competition is bigger than ever!\n");
                Console.WriteLine("\nThe revenue expected for this year's competition is {0:C}", (contCurr * 25));
            }
            else
                 if (contCurr < contLast)
            {
                Console.WriteLine("\nA tighter race this year! Come out and cast your vote!\n");
                Console.WriteLine("\nThe revenue expected for this year's competition is {0:C}", (contCurr * 25));
            }
        }
    }
}

From what I understand, you want to change this section of code so that if the user enters invalid input, it prints what the user entered, and not the uppercase version of it:

entry = Console.ReadLine().ToUpper();

while (entry != "Z")
{
    if (entry != "S" && entry != "D" && entry != "M" && entry != "O")
    {
        **Console.WriteLine("\n{0} is not a valid code.", entry);*********
        Console.WriteLine("\nPlease try again: Enter a VALID skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or 'Z' to exit");
        entry = Console.ReadLine().ToUpper();

        if (entry == "Z")
            break;
    }
}

If you want to preserve the case of the original input, then you need to save it before calling .ToUpper() .

var originalEntry = Console.ReadLine();
entry = originalEntry.ToUpper();

Then you can write out the original value in the error message:

Console.WriteLine("\n{0} is not a valid code.", originalEntry);

If the items in skill (which should be named skills , since it's a collection) are only meant to be single characters, then it should probably be a char[] instead of a string[] .

If we use char instead of string , we can easily use Console.ReadKey() to get the user input (it just reads a single character from the command line) instead of ReadLine() , which prevents the user from accidentally entering too many characters.

Then we can do comparisons of the Key in a switch statement to provide the proper response, and we can use char.ToUpper on the KeyChar to compare the input to the items in the skills array (and to output the original character they entered).

For example:

public static void Talent(char[] skills, string[] contestants)
{
    if (skills == null) throw new ArgumentNullException(nameof(skills));
    if (contestants == null) throw new ArgumentNullException(nameof(contestants));
    if (skills.Length != contestants.Length)
        throw new ArgumentException("skills must be the same length as contenstants");

    bool exit = false;

    while (!exit)
    {
        Console.Write("\nEnter a skill code ('S' 'D' 'M' 'O') to see a " +
                      "list of contestants with that skill, or 'X' to exit: ");

        ConsoleKeyInfo entry = Console.ReadKey();
        Console.WriteLine("\n");

        switch (entry.Key)
        {
            case ConsoleKey.S:
                Console.WriteLine("Contestants with talent 'Singing' are:");
                break;
            case ConsoleKey.D:
                Console.WriteLine("Contestants with talent 'Dancing' are:");
                break;
            case ConsoleKey.M:
                Console.WriteLine("Contestants with talent 'Musical' are:");
                break;
            case ConsoleKey.O:
                Console.WriteLine("Contestants with talent 'Other' are:");
                break;
            case ConsoleKey.X:
                Console.WriteLine("Exiting program...");
                exit = true;
                break;
            default:
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: '{0}' is not a valid code.", entry.KeyChar);
                Console.ResetColor();
                continue;
        }

        if (exit) break;

        bool foundSomeone = false;

        // Output the names of the contestants with the specified skill
        for (int i = 0; i < skills.Length; i++)
        {
            if (char.ToUpper(skills[i]) == char.ToUpper(entry.KeyChar))
            {
                Console.WriteLine(" - " + contestants[i]);
                foundSomeone = true;
            }
        }

        // Or if none were found, let the user know
        if (!foundSomeone) Console.WriteLine(" - <no contenstants have that skill>");
    }
}

Example Usage:

public static void Main()
{
    var skills = new[] {'S', 'D', 'D'};
    var people = new[] {"Bobby", "Sally", "James"};

    Talent(skills,people);
 
    Console.ReadKey();
}

Sample Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM