我剛剛學習c#,在繼續之前,我喜歡瞭解所有內容。爲什麼我需要2個Console.ReadLine();暫停控制檯?
我遇到的問題是我需要2 Console.ReadLine();暫停控制檯。如果我只使用1,則程序在輸入後結束。那麼爲什麼它需要2個readline方法而不是?有任何想法嗎?
請注意,在我的代碼中,我已經評論了1種readline方法,我希望我的程序能夠工作,但事實並非如此。然而,刪除評論允許程序工作,但我不明白爲什麼。
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( 「硬幣是{0} \ n \ n」 個,硬幣); 是因爲我可以看到自己的變數。這將從最終的程序中刪除。 – user4202953 2014-10-31 15:04:50