我正在处理家庭作业问题,只需在此区域提供一些帮助。我不是要求你为我做作业,只需要一些帮助,因为我在网上找不到任何其他信息。如何允许用户输入多个字符串到数组中,直到输入字母“q”
我已经创建了一个将字符串数组作为其参数的重载方法。我相信我正确地创建了这个方法,让我可以让用户输入多个存储在数组中的字符串。
我现在在静态void main中工作,并且在想要实现以允许用户输入多个字符串的代码时遇到困难,直到用户在其自己的行上输入'q',其中它会向用户显示他们刚刚输入了多少单词。
请参阅下面的代码。你将能够看到重载方法和我被卡在静态void main中的部分,该部分用星号括起来。
再次,我不是要求你做我的老板,我只需要这一部分的一些帮助。另外,这里是我需要该程序执行的一个示例。先进的谢谢您的帮助。
输入几个句子,完成句子输入时,在最后一行使用q本身。
你好,你好吗?
我很好,你呢?
很好,很好。
q
该内容共有12个单词。
输入几个句子,当完成输入句子时,在最后一行使用q本身。所有环路的
q
static void Main(string[] args)
{
Console.WriteLine("Main Menu");
Console.WriteLine("Choose from the following:");
Console.WriteLine("1. Word Count" + "\n" + "2. Vowel Counting" + "\n" + "3. Exit");
Console.Write("Enter your selection: ");
int s1 = Convert.ToInt32(Console.ReadLine());
if(s1 == 1)
{
Break:;
Console.WriteLine("Word Counter Menu");
Console.WriteLine("Choose from the following");
Console.WriteLine("1. Count the word in one sentence" + "\n" +
"2. Count the word in a paragraph" + "\n" +
"3. Parent Menu");
Console.Write("Enter your selection ");
int s2 = Convert.ToInt32(Console.ReadLine());
if(s2 == 1)
{
string sent1;
while (true)
{
Console.Write("Enter a sentence (q to quit, d for default): ");
sent1 = Console.ReadLine();
Console.WriteLine("There are " + Def1(sent1) + " words in the sentence: " + sent1);
if(sent1 == "d")
{
sent1 = "Hello";
Console.WriteLine("There are " + Def1(sent1) + " words in the sentence: " + sent1);
}
if(sent1 == "q")
{
goto Break;
}
}
}
**if (s2 == 2)
{
string sent2;
Console.WriteLine("Enter several sentences, when done entering" +
" sentences, use q by itself on the last line:");
while(true)
{
sent2 = Console.ReadLine();
if(sent2 == "q")
{
// Console.WriteLine("There are " + + " words in that text");
break;**
}
}
}
}
}
static int Def1(string d1 = "Hello")
{
int countWords = 0;
for (int i = 1; i < d1.Length; i++)
{
if (char.IsWhiteSpace(d1[i - 1]) == true)
{
if (char.IsLetterOrDigit(d1[i]) == true ||
char.IsPunctuation(d1[i]))
{
countWords++;
}
}
}
if (d1.Length > 2)
{
countWords++;
}
return countWords;
}
static int Def1(string[] d1)
{
var e = from a in d1 select a;
int cnt1 = e.Count();
return cnt1;
}
static int vow1(string v1)
{
char[] vowels = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
int total = 0;
for(int i = 0; i < v1.Length; i++)
{
if(vowels.Contains(v1[i]))
{
total++;
}
}
return total;
岂不是更好的方法使用ReadKey并将其追加到一个字符串,如果它不是q? –
任何方式,你可以给我一个例子,可能看起来像@rohit我想这样做,但没有得到我期待的结果。 –