2014-04-10 33 views
1

我在學java學習,但我需要使用C#爲我的朋友需要的項目。我從java中轉換了不在c#中工作的東西並改變了它們,但是當我運行該程序時發生了一個問題。這是它的一部分:程序只能得到1的東西

static void Main(string[] args) 
{  
     Console.WriteLine("put N"); 
     int bign= Console.Read(); 
     Console.WriteLine("put n"); 
     int n = Console.Read(); 
     Console.WriteLine("put t"); 
     int t = Console.Read(); 
} 

它只得到N然後什麼也沒有發生。 請幫我:)

回答

2

Console.Read method

Read方法阻斷其回報,而你輸入的輸入字符;當您按下Enter鍵時,它會終止。按Enter鍵將 平臺相關的行終止序列附加到您的輸入(例如, 示例,Windows附加回車換行符序列)。 對Read方法的後續調用一次檢索到您輸入的一個字符。讀取最後一個字符後,讀取 再次阻止其返回,並重復該循環。

作爲一個解決方案,可以使用Console.ReadLine()方法,並將它們與Int32.ParseInt32.TryParse方法喜歡解析到int;

int bign, n, t; 
Console.WriteLine("put N"); 
string s1 = Console.ReadLine(); 
Console.WriteLine("put n"); 
string s2 = Console.ReadLine(); 
Console.WriteLine("put t"); 
string s3 = Console.ReadLine(); 
Int32.TryParse(s1, out bign); 
Int32.TryParse(s2, out n); 
Int32.TryParse(s3, out t); 
+0

讓我怎麼把所有的人都在調試窗口? – user3519124

+0

我的意思是,在java debbuging中,你輸入一個數字並按回車。在這種情況下我需要按什麼? – user3519124

+0

@ user3519124更新了我的答案。看一看。 –