【问题标题】:C# why is it skipping my console.readline()?C#为什么跳过我的console.readline()?
【发布时间】:2011-04-17 13:15:57
【问题描述】:

所以程序运行正常,但由于某种原因,在第二次通过时,它完全跳过了 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();
        }

    }
}

【问题讨论】:

    标签: c# readline skip


    【解决方案1】:

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

    顺便说一句,通常使用起来更安全

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

    比直接比较一个空字符串。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 哇哦。我没有意识到 Read() 也会影响 ReadLine() 。谢谢
        猜你喜欢
        • 2013-03-04
        • 2010-11-15
        • 2015-01-18
        • 1970-01-01
        • 2019-05-14
        • 2015-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多