有道简单的题目:用一维数组接收用户输入的任意N个自然数,并用for循环语句来实现求和.
请告诉怎么做?

解决方案 »

  1.   

    System.Collections.ArrayList ary=new System.Collections.ArrayList();
    char ans='y';
    int result=0;
                while(ans='y' || ans='Y')
                {
                  try
                  {
                     int temp=int.Parse(Console.ReadLine());
                      Console.Write("是否继续(Y/N)?");
                      ans=char.Parse(Console.ReadLine());
                      
                  }
                }
                for(int i=0;i<ary.Count;i++)
                {
                   result+=ary[i];
                }
                Console.WriteLine(result);
                }
               Catch( Exception e)
                {
                   Console.WriteLine(e.Message);
               }    
      

  2.   

    楼上的。你的数据保存在哪里呢
    System.Collections.ArrayList ary=new System.Collections.ArrayList();
    char ans='y';
    int result=0;
                while(ans='y' || ans='Y')
                {
                  try
                  {
                     int temp=int.Parse(Console.ReadLine());
                     ary.add(temp);//先把数据放进去,然后再取出来
                      Console.Write("是否继续(Y/N)?");
                      ans=char.Parse(Console.ReadLine());
                      
                  }
                }
                for(int i=0;i<ary.Count;i++)
                {
                   result+=ary[i];
                }
                Console.WriteLine(result);
                }
               Catch( Exception e)
                {
                   Console.WriteLine(e.Message);
               }    
      

  3.   

    result+=ary[i];当然是result, 有问题吗?
      

  4.   

    System.Collections.ArrayList ary = new System.Collections.ArrayList();
                char ans = 'y';
                int result = 0;
                try
                {
                    while (ans == 'y' || ans == 'Y')
                    {                    int temp = int.Parse(Console.ReadLine());
                        ary.Add(temp);//先把数据放进去,然后再取出来
                        Console.Write("是否继续(Y/N)?");
                        ans = char.Parse(Console.ReadLine());                }                for (int i = 0; i < ary.Count; i++)
                    {
                        result += (int)ary[i];
                    }                Console.WriteLine(result.ToString());
                }            catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }