2014-02-06 43 views
1

該程序詢問用戶數組大小,然後要求用戶輸入數組值。我遇到的問題是用於讀取數組值的for循環無法正常工作。不管n的價值如何,它都會要求更多的投入。將值讀入數組

int n; 

Console.WriteLine("Enter how many values you are entering in"); 
n = Convert.ToInt32(Console.Read()); 

int[] arr = new int[n]; 
Console.WriteLine("Enter your values in"); 

for (int i = 0; i < n; i++) 
{ 
    arr[i] = Convert.ToInt32(Console.Read()); 
} 
+3

使用'Console.ReadLine()'而不是'Console.Read()'來代替輸入值。 。 – MarcinJuraszek

+0

@MarcinJuraszek你應該在猜測之前運行代碼... – Bogdan

+0

@Bogdan是什麼讓你覺得我沒有? – MarcinJuraszek

回答

3

快速和簡單的解決辦法:

使用int.Parse(Console.ReadLine())到位您Convert.ToInt32(Console.Read())

說明:

你都拿到了ASCII值你在做它的方式。例如,如果您輸入的號碼爲2,那麼您的n變量實際上將被設置爲50

+0

@MarcinJuraszek嗯我說ReadLine? –

+0

@MarcinJuraszek閱讀我的答案!它說READLINE不讀! –

+1

他不使用閱讀,他在回答中使用readline –

0

使用輸入行()得到的數字,然後使用ReadKey進入chararcters

int n; 

Console.WriteLine("Enter how many values you are entering in"); 
n = Convert.ToInt32(Console.ReadLine()); 

int[] arr = new int[n]; 
Console.WriteLine("Enter your values in"); 

for (int i = 0; i < n; i++) 
{ 
    char c = Console.ReadKey().KeyChar; 
    arr[i] = Convert.ToInt32(c.ToString()); 
} 
0

使用Console.ReadLine()而不是Console.Read()()時,未讀()或的ReadLine()。

如文檔here所示,Console.Read()只返回單個字符。您可能想要讀取整行並解析整數值。

此代碼應該做你想要什麼:

int n; 

Console.WriteLine("Enter how many values you are entering in"); 
//n = Convert.ToInt32(Console.Read()); 
n = Int32.Parse(Console.ReadLine()); 

int[] arr = new int[n]; 
Console.WriteLine("Enter your values in"); 

for (int i = 0; i < n; i++) 
{ 
    arr[i] = Int32.Parse(Console.ReadLine()); 
} 

這個作品在N = 10的輸入情況下也是如此。

0

這裏是一個更強大的錯誤檢查版本。 注:(不運行直接輸入這一點,所以我道歉,如果有輕微的語法錯誤)

 int count; 

     bool attemptedSetCount = false; 
     do 
     { 
      if (attemptedSetCount) 
      { 
       Console.WriteLine("Please enter a valid integer"); 
      } 
      Console.WriteLine("Enter how many values you are entering in"); 
      string countString = Console.ReadLine(); 
      attemptedSetCount = true; 
     } 
     while (!Int32.TryParse(countString, out count)); 

     int[] arr = new int[n]; 
     Console.WriteLine("Enter your values in"); 

     for (int i = 0; i < count; i++) 
     { 

      string valString = (Console.ReadLine()); 
      int val; 
      if(!Int32.TryParse(valString, out val)) 
      { 
       Console.WriteLine("Please enter a valid integer"); 
       i--; 
      } 
      else 
      { 
       arr[i] = val; 
      } 
     } 
0
for (int i = 0; i < n; i++) 
{ 
    string value = Console.ReadLine(); 
    int result; 
    if (int.TryParse(value, out result)) arr[i] = result; 
    else 
    { 
     Console.WriteLine("Invalid value try again."); 
     i--; 
    } 
} 
0

Console.Read函數讀取輸入的下一個字符,並將它作爲爲int值。該代碼然後將其傳遞到Convert.ToInt32 API,該API將基本上返回傳入的確切值。這意味着像1這樣具有字符值49的數字將被處理爲49。這就是爲什麼你的代碼正在讀取這麼多的值

要正確地從Console中讀取一個數字,請使用ReadLine方法,該方法返回String。這可以被傳遞到Int32.Parse並轉換爲int

Int32.Parse(Console.ReadLine()); 

或者更好的,只是抽象的這一點的方法

static int ReadInt() { 
    return Int32.Parse(Console.ReadLine()); 
} 
0

Have you read the documentation

Read方法在鍵入輸入字符時會阻止它的返回; 當您按Enter鍵時,它會終止。按Enter鍵將 平臺相關的行終止序列添加到您的輸入中(例如, Windows會附加回車換行符序列)。 隨後調用 Read方法一次檢索您輸入的一個字符。在檢索到最後的 字符後,Read再次阻止其返回,並重復循環。

注意,你不會得到-1除非你執行 下列操作之一屬性值:同時按下Control修改鍵和Z控制檯 鍵(按Ctrl + Z),這標誌着最終OF-文件條件;按一個等效鍵 ,表示文件結束條件,例如Windows中的F6功能鍵; 或將輸入流重定向到具有實際 文件結束符的源(如文本文件)。

ReadLine方法或KeyAvailable屬性和ReadKey方法是 優選使用Read方法。

如果我執行此代碼:

Console.Write("? ") ; 
int input = Console.Read() ; 
Console.WriteLine("You entered {0}.", input) ; 
Console.WriteLine("{0} is the decimal code point for the character whose glyph is '{1}.'" , input , (char)input) ; 

而且,如果是我,在?提示符下,輸入隨後return鍵的字符123

? 123<return> 

我會看到此輸出:

You entered 49. 
49 is the decimal code point for the character whose glyph is '1'. 

[注意在Windows中,可以產生在按住<ALT>鍵,鍵入「0049 and releasing the`鍵的命令提示符「1」]

假設目的是爲用戶指定你需要的代碼看起來像這樣:

static void Main() 
{ 
    int n  = ReadIntegerFromConsole("How many values do you want to enter?") ; 
    int[] values = new int[n] ; 

    for (int i = 0 ; i < values.Length ; ++i) 
    { 
    string prompt = string.Format("{0}/{1}?" , i , n) ; 

    values[i] = ReadIntegerFromConsole(prompt) ; 

    } 

    Console.WriteLine("You entered: {0}" , string.Join(", ",values)) ; 
    return ; 
} 

static int ReadIntegerFromConsole(string prompt) 
{ 
    int value ; 
    bool isValid ; 

    do 
    { 

    Console.Write(prompt) ; 
    Console.Write(' '); 

    string text = Console.ReadLine() ; 

    isValid = int.TryParse(text, out value) ; 

    prompt = "That's not an integer. Try again:" ; 
    } while (!isValid) ; 

    return value ; 
}