【发布时间】: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