我正在创建一个soundDex应用程序,我需要在输入第一个名称后询问用户第二个名字。我也想要"错误没有输入"如果用户没有输入第二个名字。我如何在我的soundDex中执行此操作?
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
}
}
}
}
答案 0 :(得分:1)
我承认,我并不完全清楚你在哪个方面遇到麻烦。但具体目标已经足够明确,并且您提供了足够的代码示例,因此......
基本问题是"我如何要求用户多次输入相同类型的数据?"基本答案与任何涉及必须重复操作的编程问题相同:将该操作概括为将执行该操作的子例程(即"方法"在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
}
}
}