开发者

Any way to repeat this process twice using a loop?

开发者 https://www.devze.com 2022-12-13 17:05 出处:网络
Is there any way to use a loop (for, while, do-while) to loop through the process below twice, but store the re开发者_开发百科sults of the second iteration through the loop in two new variables, and t

Is there any way to use a loop (for, while, do-while) to loop through the process below twice, but store the re开发者_开发百科sults of the second iteration through the loop in two new variables, and then create a second ComplexNumber object using those variable values from the second iteration?

Console.Write("Real part: ");
double realValue = double.Parse(Console.ReadLine());

Console.Write("Complex part: ");
double complexValue = double.Parse(Console.ReadLine());

ComplexNumber firstComplex = new ComplexNumber(realValue, complexValue);


Local scoping within the for loop keeps us from needing unique variables each time.

ComplexNumber[] complexNumbers = new ComplexNumber[2];

for (int i = 0; i < complexNumbers.Length; i++)
{
        Console.Write("Real part: ");
        double realValue = double.Parse(Console.ReadLine());

        Console.Write("Complex part: ");
        double complexValue = double.Parse(Console.ReadLine());
        complexNumbers[i] = new ComplexNumber(realValue, complexValue);
}


Well, sort of.

You can use an array of doubles to gather the input, but then when you get to passing them in as parameters to the constructor, you'll need to access the indexes directly, to get them out. Try and write the code yourself and ask again if you're stuck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号