C#为什么它跳过我到Console.ReadLine()? [英] C# why is it skipping my console.readline()?
问题描述
所以程序工作正常,但由于某些原因,在第二次通过,它跳过到Console.ReadLine()提示干脆。我通过调试运行,并确认它是不是循环问题,因为它实际上是在进入方法,显示的WriteLine然后完全跳过了的ReadLine,从而返回一个空白返回主()使其退出。真见鬼?任何想法?
这里是code。
使用系统;
System.Collections中使用;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间LAB4B
{
类节目
{
静态无效的主要(字串[] args)
{
串inString;
ArrayList的translatedPhrase =新的ArrayList(); DisplayInfo();
GetInput(出inString); 做
{
GetTranslation(inString,translatedPhrase);
DisplayResults(inString,translatedPhrase);
GetInput(出inString);
}而(inString =!); } 静态无效DisplayInfo()
{
Console.WriteLine(***你会被提示输入***的字符串);
Console.WriteLine(***词语的字符串将被转换成***);
Console.WriteLine(***猪拉丁语和显示结果***);
Console.WriteLine(***输入的许多字符串,你想***。);
} 静态无效GetInput(出串字)
{ Console.Write(\\ n \\ n输入一组单词或ENTER键退出:);
字=到Console.ReadLine();
} 静态无效GetTranslation(字符串originalPhrase,ArrayList的translatedPhrase)
{
INT字长;
字符串[] = splitPhrase originalPhrase.Split(); 的foreach(在splitPhrase串字)
{
字长= word.Length;
translatedPhrase.Add(word.Substring(1,字长 - 1)+ word.Substring(0,1)+AY);
}
} 静态无效DisplayResults(字符串originalString,ArrayList的translatedString)
{
Console.WriteLine(\\ n \\ nOriginal话:{0},originalString);
Console.Write(新词:);
的foreach(在translatedString串字)
{
Console.Write({0}字);
} Console.Read();
} }
}
这是因为在你的 Console.Read()
通话 DisplayResults
方法。它一般只读取一个字符。如果preSS ENTER(实际上是2个字符组合 - 回车换行)在 Console.Read()
它只是变得回车符和行饲料到达你的下一个控制台读法 - 到Console.ReadLine()
在 GetInput()
方法。因为换行符也是一个linux输入字符,到Console.ReadLine()
把它读成一条线。
So the program is working correctly, but for some reason, on the second time through, it is skipping the Console.ReadLine() prompt altogether. I ran through debug and confirmed that it isn't a loop problem as it is actually entering the method, displaying the WriteLine then completely skipping over the ReadLine, thus returning a blank back to Main() causing it to exit. What the deuce? Any ideas?
here is the code.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LAB4B
{
class Program
{
static void Main(string[] args)
{
string inString;
ArrayList translatedPhrase = new ArrayList();
DisplayInfo();
GetInput(out inString);
do
{
GetTranslation(inString, translatedPhrase);
DisplayResults(inString, translatedPhrase);
GetInput(out inString);
} while (inString != "");
}
static void DisplayInfo()
{
Console.WriteLine("*** You will be prompted to enter a string of ***");
Console.WriteLine("*** words. The string will be converted into ***");
Console.WriteLine("*** Pig Latin and the results displayed. ***");
Console.WriteLine("*** Enter as many strings as you would like. ***");
}
static void GetInput(out string words)
{
Console.Write("\n\nEnter a group of words or ENTER to quit: ");
words = Console.ReadLine();
}
static void GetTranslation(string originalPhrase, ArrayList translatedPhrase)
{
int wordLength;
string[] splitPhrase = originalPhrase.Split();
foreach (string word in splitPhrase)
{
wordLength = word.Length;
translatedPhrase.Add(word.Substring(1, wordLength - 1) + word.Substring(0, 1) + "ay");
}
}
static void DisplayResults(string originalString, ArrayList translatedString)
{
Console.WriteLine("\n\nOriginal words: {0}", originalString);
Console.Write("New Words: ");
foreach (string word in translatedString)
{
Console.Write("{0} ", word);
}
Console.Read();
}
}
}
It's because of your Console.Read()
call in DisplayResults
method. It generally reads just one character. If you press ENTER (which is actually combination of 2 characters - carriage return and line feed) on Console.Read()
it only gets carriage return character, and line feed gets to your next console reading method - Console.ReadLine()
in GetInput()
method. Since line feed character is also a linux ENTER character, Console.ReadLine()
reads it as one line.
这篇关于C#为什么它跳过我到Console.ReadLine()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!