C# 为什么它跳过我的 console.readline()?
因此,程序工作正常,但由于某种原因,在第二次执行时,它完全跳过了 Console.ReadLine() 提示。我进行了调试并确认这不是循环问题,因为它实际上正在进入该方法,显示 WriteLine,然后完全跳过 ReadLine,从而将空白返回到 Main() 导致其退出。到底是什么?有什么想法吗?
这是代码。
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();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您在
DisplayResults
方法中调用了Console.Read()
。它通常只读取一个字符。如果您在Console.Read()
上按 ENTER(实际上是 2 个字符的组合 - 回车符和换行符),它只会获得回车符,换行符会进入您的下一个控制台读取方法 -GetInput()
方法中的Console.ReadLine()
。由于换行符也是 Linux 的 ENTER 字符,因此Console.ReadLine()
将其读取为一行。It's because of your
Console.Read()
call inDisplayResults
method. It generally reads just one character. If you press ENTER (which is actually combination of 2 characters - carriage return and line feed) onConsole.Read()
it only gets carriage return character, and line feed gets to your next console reading method -Console.ReadLine()
inGetInput()
method. Since line feed character is also a linux ENTER character,Console.ReadLine()
reads it as one line.尝试将
DisplayResults
方法中的Console.Read()
更改为Console.ReadLine()
。这似乎让一切都按其应有的方式运行。Try changing the
Console.Read()
in yourDisplayResults
method toConsole.ReadLine()
. That seems to make everything behave as it should.你说的是第二次。看看你的 do-while 循环,这会失败,因为你的变量 inString 已初始化并且不为空。
顺便说一句,通常
比直接与空字符串进行比较更安全。
You said the second time round. Looking at your do-while loop, That would fall through because your variable inString is initialised and not empty.
Btw, usually safer to use
than to directly compare to an empty string.