在VS 2015中使用未分配的变量错误

时间:2015-10-05 01:56:52

标签: c# visual-studio unassigned-variable

我遇到了answer2字符串变量的问题(大约一半,我包含了整个主要方法,因为我不确定它是否会有所帮助)。编译器告诉我变量是未分配的并忽略了之前的if-else块。我不太确定如何解决它。

//scene 3之后的第一个if语句是它开始的地方(if (answer2 == "CONTACT");

static void Main(string[] args) {

    Console.Clear(); //just to clear up console

    Random theftCalc = new Random(); //calclating theft skill
    //int pcBuildSkill = 1;
    int theftSkills = theftCalc.Next(1, 10); //read above
    double dosh = 1000.0; //DOSH 
    bool hasParts = false;

    //beginning of the story
    Console.Write("You start your quest to build a PC\nbut you only have so much dosh ($" + dosh + ") ! What do you do?\n");
    Console.WriteLine("Will you WORK for more dosh or will you STEAL parts\nfrom your local Best Buy?");
    Console.WriteLine("You need to have a theft skill of more than 5 to properly steal from best buy\nand not get caught (current theft skill: " + theftSkills + ").");


    //all this is to just calculate how much you made from working
    Random rnd = new Random();
    int randomNumber = rnd.Next(100, 400);
    dosh = dosh + randomNumber;

    String answer;

    do {
        answer = Console.ReadLine();
        answer = answer.ToUpper();
        if (answer == "WORK") {
            Console.WriteLine("You put in hard work and dedication and earn $" + randomNumber + ", bringing your total dosh to $" + dosh + ". Now you can buy your parts!");

            break;

        } else if (answer == "STEAL") //the "STEAL" story 
        {
            if (theftSkills > 5) {
                Console.WriteLine("You successfully steal from Best Buy. You're a terrible person.");
                hasParts = true;
                break;
            } else {
                Console.WriteLine("You're caught stealing from Best Buy. Luckily, you get a slap on the wrists and you're sent home.");
            }
            break;
        } else {
            Console.WriteLine("That answer is invalid! Would you like to WORK or STEAL?");
        }

    } while (answer != "WORK" || answer != "STEAL");
    //scene 2

    Console.Clear();
    Console.WriteLine("SCENE 2");
    //second answer
    //have to type something to coninue?
    String answer2;
    Console.WriteLine("Now you officially have your parts and can begin building! Press enter to continue");
    if (answer == "WORK") {
        Console.WriteLine("As you begin building, you run into a few issues. You don't know how to properly hook up the PSU!\nAs you were honorable and worked for your money, you can contact Geek Squad at Best Buy without being arrested! Would you like to CONTACT Geek Squad, CONTINUE building and see if you can get past the issue, or RESEARCH online the issue you're having?");
        answer2 = Console.ReadLine();
        answer2 = answer2.ToUpper();
        do {
            if (answer2 == "CONTACT") {
                Console.WriteLine("You call up Geek Squad, but you're forced to wait on the phone for a representitive!\nEventually you get through, and they're due at your house in one hour.");
                break;
            } else if (answer2 == "CONTINUE") {
                Console.WriteLine("You continue on your own, hoping nothing goes wrong to ruin your project.");
                break;
            } else if (answer2 == "RESEARCH") {
                Console.WriteLine("Spending hours on the internet, you educate yourself properly on the inner workings of a computer and continue to build it proficiently.");
                break;
            } else {
                Console.WriteLine("That answer is invalid, would you like to CONTACT, CONINUTE, or RESEARCH?");
            }
        } while (answer2 != "CONTACT" || answer2 != "CONTINUE" || answer2 != "RESEARCH");

    } else if (answer == "STEAL") {
        Console.WriteLine("As you begin building, you run into a few issues. You don't know how to properly hook up the PSU!\nAs you stole your parts, you can't contact Geek Squad at Best Buy without being arrested! Would you like to SELL your parts, CONTINUE building and see if you can get past the issue, or RESEARCH online the issue you're having?");
        answer2 = Console.ReadLine();
        answer2 = answer2.ToUpper();
        do {
            if (answer2 == "SELL") {
                Console.WriteLine("You list your parts on craigslist as a lot, and wait for offers to come in");
                break;
            } else if (answer2 == "CONTINUE") {
                Console.WriteLine("You continue on your own, hoping nothing goes wrong to ruin your project.");
                break;
            } else if (answer2 == "RESEARCH") {
                Console.WriteLine("Spending hours on the internet, you educate yourself properly on the inner workings of a computer and continue to build it proficiently.");
                break;
            } else {
                Console.WriteLine("That answer is invalid, would you like to CONTACT, CONINUTE, or RESEARCH?");
            }
        } while (answer2 != "SELL" || answer2 != "CONTINUE" || answer2 != "RESEARCH");
    }

    //scene 3 endings
    Console.Clear();
    Console.WriteLine("SCENE 3");

    //picks between two endings for each
    Random end = new Random();
    int ending = end.Next(0, 10);
    if (answer2 == "CONTACT") {
        if (ending > 5) {
            Console.WriteLine("Geek Squad arrives, and assembles your PC for you. Congratdulations! You've won! THE END");
        } else {
            Console.WriteLine("Geek Squad never arrives...you start to worry. Upon calling their center, it turns out their car was summoned to R'lyeh and consumed by Cthulhu. THE END");
        }

    } else if (answer2 == "SELL") {
        if (ending > 5) {
            Console.WriteLine("You craigslist ad is answered! You're still a horrible person for stealing. You meet the buyed, and what do you know it's the police. You're arrested for Grand Larceny and sentenced to four years in prison. THE END");
        } else {
            Console.WriteLine("A response for your ad comes in! You go to meet the buyer, and he stabs you to re-steal your stolen goods. Thiefs never win, you horrible person. THE END");
        }
    } else if (answer2 == "CONTINUE") {
        if (ending > 7) {
            Console.WriteLine("Your attempts at winging it have paid off! Everything is assembled, running fine! Congratdulations! THE END");
        } else {
            Console.WriteLine("After hours and hours of winging it, your PC lies on the ground in a mess of aluminum and silicon. Maybe you should've gotten professional help. THE END");
        }
    } else if (answer2 == "RESEARCH") {
        Console.WriteLine("After spending a few hours on PC forums, you gain a wealth of knowledge about building machines. You adeptly assemble yours in no time, and have it running. THE END");
    }

}

1 个答案:

答案 0 :(得分:2)

您在第2部分声明answer2

时从answer2读取值console
answer == "WORK"

answer == "STEAL"

但如果答案不是worksteal,那么answer2将保持未分配状态。

试试这个

String answer2=string.Empty;