我希望第 23 行中的 if 检查用户是否输入了字母 x,如果输入了,代码停止并对已输入的数字进行乘法运算。我怎样才能做到这一点?我尝试了几种方法,但我一直在搞砸其他事情,这是代码的最终格式,我只需要完成检查即可。先感谢您
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();
}
}
}
}
编辑:这个问题的另一个解决方案很容易是:
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();
}
}