C#询问用户另一个输入

本文关键字:另一个 输入 用户 | 更新日期: 2023-09-27 18:21:58

我正在创建一个soundDex应用程序,在用户输入第一个名称后,我需要向用户询问第二个名称。如果用户不输入第二个名称,我还希望"error no input"。我该如何在我的声音Dex中做到这一点?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoundDexFinal
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = null;
            bool good = false;

            Console.WriteLine("SoundDex is a phonetic algorithm which allows a user to encode words which sound the same into digits."
                + "The program allows the user essentially to enter two names and get an encoded value.");
            while (!good) // while the boolean is true
            {
                Console.WriteLine("Please enter a name -> "); // asks user for an input
                input = Console.ReadLine();
                // Make sure the user entered something
                good = !string.IsNullOrEmpty(input); // if user enters a string which is null or empty
                if (!good) // if boolean is true
                    Console.WriteLine("Error! No input."); // displays an error to the user
            }
            soundex soundex = new soundex(); // sets new instance variable and assigns from the method
            Console.WriteLine(soundex.GetSoundex(input)); // gets the method prior to whatever the user enters   
            Console.ReadLine(); // reads the users input

        }
        class soundex
        {
            public string GetSoundex(string value)

            {
                value = value.ToUpper(); // capitalises the string
                StringBuilder soundex = new StringBuilder(); // Stringbuilder holds the soundex code or digits
                foreach (char ch in value) // gets the individual chars via a foreach which loops through the chars
                {
                    if (char.IsLetter(ch))
                        AddChar(soundex, ch); // When a letter is found this will then add a char 
                } // soundex in (parameter) is for adding the soundex code or digits 
                return soundex.ToString(); //return the value which is then converted into a .String() 
            }
            private void AddChar(StringBuilder soundex, char character) //encodes letter as soundex char and this then gets appended to the code
            {
                string code = GetSoundexValue(character);
                if (soundex.Length == 0 || code != soundex[soundex.Length - 1].ToString())
                    soundex.Append(code);
            }
            private string GetSoundexValue(char ch)
            {
                string chString = ch.ToString();
                if ("BFPV".Contains(chString))  // converts this string into a value returned as '1'
                    return "1";
                else if ("CGJKQSXZ".Contains(chString))  // converts this string into a value returned as '2'
                    return "2";
                else if ("DT".Contains(chString))  // converts this string into a value returned as '3'
                    return "3";
                else if ("L".Contains(chString))  // converts this string into a value returned as '4'
                    return "4";
                else if ("MN".Contains(chString))  // converts this string into a value returned as '5'
                    return "5";
                else if ("R".Contains(chString))  // converts this string into a value returned as '6'
                    return "6";
                else
                    return ""; // if it can't do any of these conversions then return nothing
            }
        }
    }

}

C#询问用户另一个输入

我承认,我不完全清楚你到底遇到了什么问题。但具体目标已经说得很清楚了,而且您已经提供了一个足够的代码示例,所以…

基本问题是"我如何要求用户多次输入相同类型的数据?"基本答案与任何涉及重复操作的编程问题相同:将该操作概括为执行该操作的子例程(即C#中的"方法"),并在每次需要执行该操作时调用该子例程。

例如:

class Program
{
    static void Main(string[] args)
    {
        string input1, input2;
        Console.WriteLine("SoundDex is a phonetic algorithm which allows a user to encode words which sound the same into digits."
            + "The program allows the user essentially to enter two names and get an encoded value.");
        input1 = GetValidInput("Please enter a name -> ");
        input2 = GetValidInput("Please enter a second name -> ");
        soundex soundex = new soundex(); // sets new instance variable and assigns from the method
        Console.WriteLine(soundex.GetSoundex(input1)); // gets the method prior to whatever the user enters
        // do whatever you want with input2 as well
        Console.ReadLine(); // reads the users input
    }
    static string GetValidInput(string prompt)
    {
        while (true)
        {
            string input;
            Console.WriteLine(prompt); // asks user for an input
            input = Console.ReadLine();
            // Make sure the user entered something
            if (!string.IsNullOrEmpty(input))
            {
                return input;
            }
            Console.WriteLine("Error! No input."); // displays an error to the user
        }
    }
}