【问题标题】:Why do I need 2 Console.ReadLine(); to pause the console?为什么我需要 2 Console.ReadLine();暂停控制台?
【发布时间】:2014-10-31 15:03:20
【问题描述】:

我刚刚学习 c#,我喜欢在继续之前了解所有内容。

我遇到的问题是我需要 2 Console.ReadLine();暂停控制台。如果我只使用 1,则程序在输入后结束。那么为什么它需要 2 个 readline 方法而不是呢?有任何想法吗?

请注意,在我的代码中,我已经注释掉了 1 个 readline 方法,这是我希望我的程序工作的方式,但它没有。但是删除 cmets 可以让程序工作,但我不明白为什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoinFlip
{
    class Program
    {
        static void Main(string[] args)
        {

            Random rng = new Random();
            Console.WriteLine(@"

This program will allow you to guess heads or tails on a coin flip.

Please enter h for heads, or t for tails and press Enter: ");

            char userGuess = (char)Console.Read();
            int coin = rng.Next(0,2);

            Console.WriteLine("Coin is {0}\n\n", coin);


            if (coin == 0 && (userGuess == 'h' || userGuess == 'H'))
            {

                Console.WriteLine("It's heads! You win!");

            }
            else if (coin == 1 && (userGuess == 't' || userGuess == 'T'))
            {
                Console.WriteLine("It's tails! You win!");

            }
            else if (userGuess != 't' && userGuess != 'T' && userGuess != 'h' && userGuess != 'H') 
            { 
                Console.WriteLine("You didn't enter a valid letter"); 
            }

            else
            {

                if (coin == 0) { Console.WriteLine("You lose mofo. The coin was heads!"); }
                if (coin == 1) { Console.WriteLine("You lose mofo. The coin was tails!"); }

            }
            Console.ReadLine();
            //Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 还要注意这一行:Console.WriteLine("Coin is {0}\n\n", coin);是为了让我自己可以看到变量号。这将从最终程序中删除。

标签: c# console.readline


【解决方案1】:

您正在使用Console.Read(),它在用户点击返回后读取单个字符。但是,它只消耗单个字符 - 这意味着该行的其余部分(即使它是空的)仍在等待消耗......Console.ReadLine() 正在做的事情。

最简单的解决方法是更早地使用Console.ReadLine()

string userGuess = Console.ReadLine();

.. 然后可能检查猜测是单个字符,或者只是将所有字符文字(例如't')更改为字符串文字(例如"t")。

(或者按照Servy的建议使用Console.ReadKey()。这取决于您是否希望用户点击返回。)

【讨论】:

  • 干杯伙计们,哇,你们的速度超级快,而且解释得很好。根据您的解释,我在底部添加了以下代码来测试您在说什么: Console.WriteLine(Console.ReadLine()); Console.ReadLine();然后我写了tomato 果然用t 表示尾巴,然后在console.writeline 上读出“omato”
  • 我还买了一本名为 C# Indepth 3rd edition 的书,我打算在掌握了基础知识后再阅读。不知道你是不是写它的那个人。
  • @user4202953:是的,我是。希望你喜欢它:)
  • 哈哈,以最书呆子的方式,我只想说,我的问题得到了畅销书作者的回答,我很受宠若惊。谢谢你。 :)
【解决方案2】:

简短的回答,不要使用Console.Read。在您提交一行文本之前,它无法读取任何内容,但它只读取该行文本的第一个字符,将该行的其余部分留给进一步的控制台输入,例如调用Console.ReadLine。使用Console.ReadKey 而不是Console.Read 读取单个字符。

【讨论】:

  • 感谢您的回答。虽然我更好地理解了乔恩的解释。您确实向我介绍了一种新方法 *Console.ReadKey,我一定会使用它。谢谢你。 :)
【解决方案3】:

第一个 Console.ReadLine()Enter 键使用,因此程序结束。
试试这个,而不是 Console.Read()

    var consoleKeyInfo = Console.ReadKey();
    var userGuess = consoleKeyInfo.KeyChar;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多