簡體   English   中英

C#While Loop繼續重用第一個用戶輸入;

[英]C# While Loop keeps reusing the first user input;

正如這里許多人所說的,我是一個非常新的程序員,試圖學習基礎知識,以便將來擁有一些技能。 我正在觀看Bob Tabor在C#上的Channel9視頻,並且學到了很多東西,但是當我開始自己進行創作時,我發現有些事情我不理解。 我正在為一個非常簡單的文字游戲編寫代碼,並且我很清楚我的代碼可能是混亂的,冗余的,或兩者兼而有之。

話雖如此,它可以完美執行我想要的工作,省去了一個小小的麻煩。 如果我以“是”或“否”開頭輸入,則它會繼續進行。 問題是用戶是否輸入了錯誤的選項; 我希望他們收到一條消息,說是或否,然后回到開頭再試一次。 但是,當它執行完畢,獲取該消息並返回時,它將僅從第一時間重新應用輸入,而我將陷入無限循環。 我知道問題出在哪里,但是由於我沒有經驗,所以我不確定如何解決該問題。 我將嘗試復制/粘貼我的代碼,以便您可以看到我的問題。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FirstSoloAttempt
{
  class Program
  {
    static void Main(string[] args)
    {
      bool incorrectAnswer = true; //if user inputs anything other than yes or no, reloop to beginning
      Console.WriteLine("Do you want to play a game?");

      string answer1 = Console.ReadLine(); //somehow, need to allow this input to be changed second time

      while (incorrectAnswer)
      {
        if (answer1.Contains("yes"))
        {
          incorrectAnswer = false;
          Console.WriteLine("Great! Let's begin. Which door is the new car behind? It is behind door 1, door 2, or door 3?");
          string answer2 = Console.ReadLine();

          if (answer2.Contains("door 1"))
          {
            Console.WriteLine(" Your new car is a new 2016 Chevy Corvette! Congratulations!");
            Console.WriteLine("Are you satisfied with your new car?");

            string answer3 = Console.ReadLine();

            if (answer3.Contains("yes"))
            {
              Console.WriteLine("Fantastic!");
            }
            else
            {
              Console.WriteLine("I'm sorry you are not satisfied. You may return it for a different car.");
            }
          }
          else
          {
            Console.WriteLine("Oh! Bummer! You didn't win. Thanks for playing!");
          }
        }
        else if (answer1.Contains("no"))
        {
          incorrectAnswer = false;
          Console.WriteLine("Alright then! Goodbye!");
        }
        else
        {
          Console.WriteLine("I'm sorry, I didn't understand that. Please answer with yes or no.");
        }

        Console.ReadLine();
      }
    }
  }
}

基本上,我的代碼不允許我接收新輸入以更改程序循環的結果。 我知道ti與從第一個字符串answer1讀取輸入的代碼有關,那么如何為以后的所有嘗試更改它?

感謝您的任何幫助! 編碼對我而言很快變得很有趣,而其他人提供幫助的社區也帶來了很大的積極影響。

while循環內的Console.ReadLine永遠不會分配給任何東西。

更改

 Console.ReadLine();

answer1 = Console.ReadLine(); 

您可能應該在您的主要空白中使用類似的內容。 這將使您能夠繼續閱讀輸入內容,直到用戶鍵入包含“是”的內容為止。

string answer1 = Console.ReadLine();
while (answer1.Contains("yes") != true)
{
    answer1 = Console.ReadLine();
}

這可能是一個很好的解決方案,因為一旦為答案重新分配了變量,就不必為下一個響應進行硬編碼,並且會一直持續到您的客戶鍵入“是”。

do-while循環更適合您的情況。

do
  {
    string answer1 = Console.ReadLine();
    //your existing code from while loop ...
  }while(incorrectAnswer)

只需將更改您的else語句添加到以下內容中

else
{
      Console.WriteLine("I'm sorry, I didn't understand that. Please answer with yes or no.");
      Console.WriteLine("Do you want to play a game?");

      answer1 = Console.ReadLine(); 
 }

后一個Console.ReadLine()不會更改第一個變量的狀態

更改為var answer1 = Console.ReadLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM