我遇到问题创建一个循环,检查变量10和50之间是否有5个变量。我相信我已经设置了正确的编码,但是我收到一个错误,说我无法将int转换为布尔。这是我现在拥有的:
string userName = "";
int v1, v2, v3, v4, v5 = 0;
float avg;
float variance;
Console.WriteLine("What is your name?");
userName = Console.ReadLine();
Console.WriteLine();
int i = 1;
while (i <= 5)
{
int InputCheck = 0;
Console.WriteLine("Please input a number {0} between 10 and 50;", i);
InputCheck = Convert.ToInt32(Console.ReadLine());
if (InputCheck >= 10 && InputCheck <= 50)
{
if (i >= 10 && i <= 50)
i++;
if (i != 1)
{
InputCheck = v1;
}
if (i != 2)
{
InputCheck = v2;
}
if (i == 3)
{
InputCheck = v3;
}
if (i == 4)
{
InputCheck = v4;
}
if (i == 5)
{
InputCheck = v5;
}
if (InputCheck < 10 || InputCheck > 50)
{
Console.WriteLine("The number you entered is either to high or to low please re-enter a number:");
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
我不是百分百肯定,但我认为你的错误来自这一行:
Console.WriteLine("Please input a number {0} between 10 and 50;", i);
你给了一个 int 'i',它需要一个布尔值。 也许这会有所帮助:http://msdn.microsoft.com/en-us/library/70x4wcx1.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
至于你的其余代码:
假设您声明:
int v[5]; /* creates array v[0], v[1], ... v[4] */
int i = 0;
while (i<=4){
/* internal code */
}
这样的事情应该有效:
/* internal code */
if (InputCheck >= 10 && InputCheck <= 50)
{
v[i] = InputCheck;
i++;
}
else if (InputCheck < 10 || InputCheck > 50)
{
Console.WriteLine("The number you entered is either to high or to low please re-enter a number:");
}