将包含未知变量的数组放入另一个数组中

时间:2015-02-20 16:21:34

标签: arrays multidimensional-array

此代码的目的是定义平方和的根。 我无法弄清楚如何将我放入j。请帮忙。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int input, som, i=0;
            int j = 0;
            double answer;
            Boolean gaDoor= true;
            int  [] array = new int [24];


            while (gaDoor)
            {
                Console.Write("Specify a positive integer");
                input = Convert.ToInt32(Console.ReadLine());
                if (input == -1)
                {
                    gaDoor = false;

                }
                else
                {
                    if (input >= 0)
                    {
                        array[i] = input;
                        i++;
                    }
                    else
                    {
                        Console.WriteLine("Specify a positive integer ");
                    }
                }



            }

            while (j<i) 
            {
                sum = array [j] ^ 2;
                answer = Math.Sqrt(sum);
                Console.Write(answer);

            }


            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

using System;

namespace Test
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            int[] invoer = new int[24];
            double[] resultaat = new double[24];
            double totaal = 0;
            double wortel = 0;

            int commando = 0;
            int teller = -1;
            try {
                // Keep going until a negative integer is entered (or a 0)
                while ((commando = Convert.ToInt32 (Console.ReadLine ())) > 0) {
                    teller++;
                    invoer [teller] = commando;
                }
            } catch (FormatException) {
                // Not a number at all.
            }

            teller = -1;
            foreach (int i in invoer) {
                teller++;
                resultaat [teller] = Math.Pow (invoer [teller], 2);
                totaal += resultaat [teller];
                if (invoer [teller] > 0) {
                    Console.WriteLine ("Invoer: {0}, Resultaat: {1}", invoer [teller], resultaat [teller]);
                }
            }

            wortel = Math.Sqrt (totaal);
            Console.WriteLine ("Totaal: {0}, Wortel: {1}", totaal, wortel);
        }
    }
}