2015-10-05 33 views
0

我遇到了answer2字符串變量的問題(大約一半的時候,我包括了整個主要方法,因爲我不確定它是否有幫助)。編譯器告訴我該變量未被分配,並忽略前一個if-else塊。我不太清楚如何解決它。在VS 2015中使用未分配的變量錯誤

第一,如果//scene 3語句後面是它開始(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"); 
    } 

} 

回答

2

你在第2

聲明answer2和值分配給answer2console

answer == "WORK" 
閱讀

answer == "STEAL" 

但是如果回答不是對於worksteal不是如此回答,那麼answer2將保持未分配狀態。

嘗試這樣

String answer2=string.Empty; 
+0

謝謝你,這個工作! – beefoak

+0

@beefoak歡迎您:) –

相關問題