Let us say input for the while(true) is x and y coordinates. As long as user provide x and y coordinates loop will continue. Any keystroke will terminate the loop and list relevant x and y coordinates. Following is an incomplete program please complete it.
using System;
class Program
{
static void Main(string[] args)
{
int i = 0;
while (true)
{
Console.Write("X = ");
double x = int.Parse(Console.ReadLine());
Console.Write("Y = ");
double y = int.Parse(Console.ReadLine());
Console.WriteLine();
i++;
}
x[i] = x;
y[i] = y;
double[] x = new double[i];
double[] y = new double[i];
Console.WriteLine("X Y");
Console.ReadKey();
}
}
Loading
VulpesPosted Sep 29, 2014, 4:05 PM
The first one is how to distinguish between keystrokes used to enter the coordinates and the keystroke needed to end the loop?
My proposed solution is that, if a keystroke is entered which means that the input cannot be parsed to a double, then it will break the loop.
The second problem is that you need to store the coordinates as they are entered so you can print them out later. However, even if you declare them before the while loop begins, arrays are not really suitable for this as you don't know in advance how many coordinate pairs are going to be entered.
My proposed solution to this is to use a List rather than an array as this increases its size dynamically.
Here's the code:
Posted Sep 29, 2014, 4:27 PM