示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace poleProstokata
{
class Program
{
static void Main(string[] args)
{
double a;
double b;
double result;
Console.WriteLine("Enter value A");
a = Console.Read();
Console.WriteLine("Enter value B");
b = Console.Read();
result = a * b;
Console.WriteLine("result value "+result);
Console.ReadLine();
}
}
}
现在我的问题在于Console.ReadLine()。如果我想阅读var'A'程序乘以a * a并且不想读取变量'B',那么我的问题是: 如何从键盘读取多个变量并将其分配给不同的变量?
答案 0 :(得分:-1)
您需要将input strings
转换并验证为doubles
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace poleProstokata
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter value A");
double a;
if (Double.TryParse(Console.ReadLine(), out a)) {
a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter value B");
double b;
if (Double.TryParse(Console.ReadLine(), out b)) {
Console.WriteLine("Result value: "+ (a * b));
Console.Read();
} else {
Console.WriteLine("Invalid input for B!");
}
} else {
Console.WriteLine("Invalid input for A!");
}
}
}
}