2014-11-22 119 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的,现在你可以只添加你输入的值。