Reputation: 53
I want the if from line 23 to check if the user has input the letter x and if they did, the code to stop and do the multiplication for the numbers already inputed. How can I do that? I've tried several methods, but I keep on messing other things up, this is the final format of the code, in which I just need to have that check-up done. Thank you in advance
using System;
class Program
{
static void Main(string[] args)
{
string[] array = new string[100];
bool a = Console.ReadKey(true).Key == ConsoleKey.X;
if (a==true)
{
Console.WriteLine("1");
Console.ReadLine();
}
else
{
while(a==false)
{
double multiply = 1;
for (int i= 0; i<array.Length; i++)
{
array[i] = Convert.ToString(Console.ReadLine());
if (a == true) break;
multiply *= Convert.ToDouble(array[i]);
}
Console.WriteLine(multiply);
Console.ReadLine();
}
}
}
}
EDIT: Another solution to this problem and quite easier is:
class Program
{
static void Main(string[] args)
{
int product = 1;
string inputData = Console.ReadLine();
while(inputData != "x")
{
int number = Convert.ToInt32(inputData);
product *= number;
inputData = Console.ReadLine();
}
Console.WriteLine(product);
Console.ReadLine();
}
}
Upvotes: 1
Views: 353
Reputation: 17605
Apparently your current approach does not yield the desired result because a
is never set inside of the loop; you have to re-set a
accordingly inside of the loop.
Upvotes: 1
Reputation: 34150
Read from console to a variable, if it is x
break the loop otherwise set it to your array:
for (int i= 0; i<array.Length; i++)
{
string input = Convert.ToString(Console.ReadLine());
if (input == "x" || input == "X") break;
else array[i] = input;
multiply *= Convert.ToDouble(array[i]);
}
Upvotes: 3