2014-11-22 116 views
0

我想在控制檯應用程序中使用此代碼創建一個部分數組。我想要做的是在一個Console.ReadLine上輸入多個值(例如測試分數),取得輸入數字的總和,但是如果用戶輸入小於LIMIT,例如輸入5個值,但是有空間總共10個,它會將這5個值加起來。如何在C中創建一個部分填充的數組#

我希望能夠在一行上使用數組輸入多個值,但如果我不輸入每個參數的值int [] scores = {0, 1, 2, ...];它應該能夠合計用戶輸入的數字,並忘記其餘的。例如,如果我在一行中輸入56 76 86,則輸入0終止陣列,它將加起來56 76 86,而不需要其他數字來填充陣列。

class Program 
    { 
     const int LIMIT = 10; 

     static void Main(string[] args) 
     { 
      //Declarations: 
      //Array Size 
      //Array Scope 




      int[] examScores = new int[LIMIT]; 

      //Define an Array of integers: 
      int testNum = 1; 
      int [] scores = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

      //1. Ask User Method: 
       //a.)Ask user to input numbers. 
       //b.)Save user numbers in an array 

      Console.WriteLine("Input all of your test scores as the program prompts of"); 
      Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)"); 


      //Purpose of this for method is to get user input and save to 
      //an array. 

      for (int i = 0; i < scores.Length; i++) 
      { 
       Console.WriteLine("\nEnter test score #{0}", testNum); 
       scores[i] = int.Parse(Console.ReadLine()); 
      } 
      PrintScores(scores); 




      Console.Read(); 
     }//End Main. 

     //2. AddSum Method. 
     //Purpose: Take users input and add all numbers together. 
     //Paramiters: Array numbers from Main saved as PrintScores 
     //Returns: None 
     //Prints: Sum of Scores. 
     static void PrintScores(int[] scr) 
     { 
      int result = 0; 

      for (int i = 0; i < scr.Length; i++) 
      { 
       result += scr[i]; 
      } 

      Console.WriteLine("\n--------------------------------------"); 
      Console.WriteLine("Sum of your test scores equal: {0}", result); 


     } 
    } 
} 
+0

你的實際問題是什麼?你有什麼特別的困難?請參閱http://stackoverflow.com/help/how-to-ask – 2014-11-22 03:13:18

+0

我希望能夠使用數組輸入多個值,但是如果我不輸入每個參數的值{int [] scores = {0,1 ,2,...];}它應該能夠將用戶輸入的數字相加,並忘記其餘的。例如,如果我在一行中輸入56 76 86,則輸入0終止陣列,它將加起來56 76 86,而不需要其他數字來填充陣列。 – 2014-11-22 03:28:32

+0

@GoodyGoodmansen然後你應該使用'List '而不是數組。數組必須具有固定的大小;列表不。 – 2014-11-22 03:42:38

回答

0

看一看的string.Split()方法,其將字符串轉換成基於一個定界符的多個部件。

在這種情況下,只需讀取單個Console.ReadLine()中的所有文本,然後將這些字符串拆分爲數組,然後解析它們,然後將它們插入到examScores數組中。

string line = Console.ReadLine(); //Read the line of scores (Ex: 89 25 87 98) 
int[] scores = line.Split(' '); //Split it between the spaces, into an array 

for (int i = 0; i < input.Length; i++) //For each element in this array, set the score array to the same 
    examScores[i] = scores[i]; 

它只會複製輸入分數的數字,其餘的將保持不變。

+0

我從int []分數= line.Split();指出它不能隱式地將字符串轉換爲int。 – 2014-11-22 03:17:20

0

我還不確定我明白你有什麼問題。但我認爲,你的代碼以下更改要做到你想要什麼:

List<int> scores = new List<int>(); 

Console.WriteLine("Input all of your test scores as the program prompts of"); 
Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)"); 

string line; 

Console.WriteLine("\nEnter test score #1"); 
while ((line = Console.ReadLine()) != "") 
{ 
    scores.Add(int.Parse(line)); 
    Console.WriteLine("\nEnter test score #{0}", scores.Count + 1); 
} 
PrintScores(scores.ToArray()); 

以上將允許用戶輸入一個分數每行(按你原來的代碼示例),而當他們只需按進入沒有輸入任何東西(即他們輸入一個空行),它將停止嘗試閱讀更多的分數,並會調用你的PrintScores()方法來添加它們。

編輯:根據您的澄清,原來的代碼示例並不代表你想要的行爲,這裏是另一種實現:

Console.WriteLine("Input all of your test scores as the program prompts of"); 
Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)"); 

Console.WriteLine("\nEnter test scores: "); 
int[] scores = Console.ReadLine().Split(' ').Select(text => int.Parse(text)).ToArray(); 
PrintScores(scores); 

這會讀取來自用戶的線,每個輸入的整數之間有一個空格。它將單個整數從文本格式轉換爲實際的int,最後將結果複製到實際的int[]中,以便將其傳遞給您的PrintScores()方法。

如果您希望允許用戶在每個數字之間輸入任意數量的空格,請參閱String.Split()方法的文檔以選擇通過以從拆分結果中刪除空元素的選項。

+0

我想分割這個,以便所有的值都輸入到一行中,然後加在一起,但能夠部分輸入。 – 2014-11-22 04:25:05

+0

請參閱我的編輯。 – 2014-11-22 04:44:27

0

我想我明白:

for (int i = 0; i < scores.Length; i++) 
{ 
    Console.Write("\nEnter test score #" +(i+1) +": "); 
    scores[i] = int.Parse(Console.ReadLine()); 
    if (scores[i] == 0) 
    { 
     while (i < scores.Length) 
     { 
      scores[i]=0; 
      i++; 
     } 

    } 

} 
PrintScores(scores); 

這樣,當你輸入一個0,這將填補陣列的其餘部分爲0的,現在你可以只添加你輸入的值。