C# 为什么它跳过我的 console.readline()?

发布于 2024-09-24 17:00:40 字数 2216 浏览 3 评论 0原文

因此,程序工作正常,但由于某种原因,在第二次执行时,它完全跳过了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云胡 2024-10-01 17:00:40

这是因为您在 DisplayResults 方法中调用了 Console.Read() 。它通常只读取一个字符。如果您在 Console.Read() 上按 ENTER(实际上是 2 个字符的组合 - 回车符和换行符),它只会获得回车符,换行符会进入您的下一个控制台读取方法 - GetInput() 方法中的 Console.ReadLine()。由于换行符也是 Linux 的 ENTER 字符,因此 Console.ReadLine() 将其读取为一行。

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.

几度春秋 2024-10-01 17:00:40

尝试将 DisplayResults 方法中的 Console.Read() 更改为 Console.ReadLine()。这似乎让一切都按其应有的方式运行。

Try changing the Console.Read() in your DisplayResults method to Console.ReadLine(). That seems to make everything behave as it should.

冷情妓 2024-10-01 17:00:40

你说的是第二次。看看你的 do-while 循环,这会失败,因为你的变量 inString 已初始化并且不为空。

顺便说一句,通常

do
{
} while (!String.IsNullOrEmpty(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

do
{
} while (!String.IsNullOrEmpty(inString));

than to directly compare to an empty string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文