我希望if从第23行开始检查用户是否输入了字母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();
}
}
从控制台读取一个变量,如果它x
中断了循环,则将其设置为您的数组:
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]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句