当前正在使用C#,并且在循环时遇到问题

时间:2019-09-28 22:04:13

标签: c# if-statement while-loop

我目前在循环以下代码时遇到麻烦,因此else部分再次提出了问题。

String name, wtd, act, trav;        
Console.WriteLine("What is your name?");
name = Console.ReadLine();
Console.WriteLine("Hello {0}, What do you want to do today?", name);
Console.WriteLine("1) Action\n2) Chilling\n3) Danger\n4) Good Food\n");
int result, ppl;
result = Convert.ToInt32(Console.ReadLine());
if (result == 1)
{
    wtd = "action";
    act = "Stock Car Racing";
}
else if (result == 2)
{
    wtd = "chilling";
    act = "Hiking";
}
else if (result == 3)
{
    wtd = "danger";
    act = "Skydiving";
}
else if (result == 4)
{
    wtd = "good food";
    act = "to Taco Bell";
}
else
{
    wtd = "";
    act = "";
    Console.WriteLine("I do not understand. Please select again");
}
Console.WriteLine("Okay. If you are in the mood for " + wtd + ", then you should go " + act + "and travel in " + trav +".");

*其余代码工作正常。 trav变量可以正常工作

我尝试使用while循环,但是它总是返回错误,或者没有按照我想要的方式运行。使用while循环时,最常见的错误之一是“ 使用未分配的局部变量wtd ”和“ 使用未分配的局部变量act ”。这些错误发生在代码的最后Console.WriteLine部分。

我还需要根据使用的输入来定义变量,并且希望在有无效输入(如列出的值(1,2,3,4)之外的其他任何值)的情况下循环。

3 个答案:

答案 0 :(得分:1)

使用while循环来不断询问输入是正确的,直到正确为止。

类似的事情应该起作用:

int result = Convert.ToInt32(Console.ReadLine());
int ppl;
while (result < 1 && result > 4)
{
    Console.WriteLine("I do not understand. Please select again");
    result = Convert.ToInt32(Console.ReadLine());
}

对于错误,您遇到的是“使用未分配的局部变量wtd”,仅表示在尝试最后打印时,变量可能没有赋值。通过在顶部声明值时简单地初始化值即可解决此问题:

String name = "", wtd = "", act = "", trav = "";

答案 1 :(得分:0)

对于未分配的局部变量消息,如果您在循环外部声明变量,而只是在循环内部使用它们,则会出现未分配的局部变量信息消息,这不会破坏应用程序,只是在告诉您您没有在当前范围内使用变量。这是一个很好的功能,它将帮助您从未使用的变量中清除代码。

对于您的代码,您可以这样做:

string name = string.Empty, wtd = string.Empty, act = string.Empty, trav = string.Empty;
int result = 0, ppl = 0;
bool isTrue = true;
Console.WriteLine("What is your name?");
name = Console.ReadLine();
Console.WriteLine("Hello {0}, What do you want to do today?", name);
while (isTrue)
{
    Console.WriteLine("1) Action\n2) Chilling\n3) Danger\n4) Good Food\n");
    var isValidInt = int.TryParse(Console.ReadLine(), out result);
    if(isValidInt)
    {
        if (result == 1)
        {
            wtd = "action";
            act = "Stock Car Racing";
            isTrue = false;
        }
        else if (result == 2)
        {
            wtd = "chilling";
            act = "Hiking";
            isTrue = false;
        }
        else if (result == 3)
        {
            wtd = "danger";
            act = "Skydiving";
            isTrue = false;
        }
        else if (result == 4)
        {
            wtd = "good food";
            act = "to Taco Bell";
            isTrue = false;
        }
        else
        {
            wtd = "";
            act = "";
            Console.WriteLine("I do not understand. Please select again");
        }
    }
    else
    {
        Console.WriteLine("Use numbers only");
    }
}
Console.WriteLine("Okay. If you are in the mood for " + wtd + ", then you should go " + act + "and travel in " + trav + ".");

isTrue变量将通知循环继续循环,例如,如果用户输入1,它将变量设置为false,以结束循环(如果用户输入)任何大于4的数字,都将其保留在循环中,然后将重新开始。

我还用此result = Convert.ToInt32(Console.ReadLine());替换了行int.TryParse(Console.ReadLine(), out result);,您应该始终使用TryParse而不是直接转换整数。 TryParse将检查字符串,如果它是一个有效的整数,则将对其进行解析,然后它将返回true(因为它是整数),而out将是已解析的整数。如果解析失败,它将返回false。

这样,您将确保仅获取整数。

答案 2 :(得分:0)

您应该使用Int32.TryParse来确认输入是有效的int。通过使用do while,您可以检查数字在1到4之间(包括1和4)。我还建议使用switch代替if elseif

int result, ppl;
String name, wtd, act, trav;        
Console.WriteLine("What is your name?");
name = Console.ReadLine();
Console.WriteLine("Hello {0}, What do you want to do today?", name);
do
{
    Console.WriteLine("1) Action\n2) Chilling\n3) Danger\n4) Good Food\n");
    Console.WriteLine("I do not understand. Please select again");
} while (!Int32.TryParse(Console.ReadLine(), out result) || result < 1 || result > 4)
switch (result)
{
    case 1:
        wtd = "action";
        act = "Stock Car Racing";
        break;
    case 2:
        wtd = "chilling";
        act = "Hiking";
        break;
    case 3:
        wtd = "danger";
        act = "Skydiving";
        break;
    case 4:
        wtd = "good food";
        act = "to Taco Bell";
        break;
    default:
        wtd = act = "";
}
Console.WriteLine("Okay. If you are in the mood for " + wtd + ", then you should go " + act + "and travel in " + trav +".");