簡體   English   中英

C# IF 語句雙結果

[英]C# IF statement double result

我是C#學習的新手,我遵循了一個教程,然后每次我的 IF 語句為假時嘗試自己拋出一條錯誤消息。 這是代碼:

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



namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            string SecretWord = "Banana";
            string Guess = "";
            int GuessCount = 0;
            int GuessLimit = 4;
            bool OutOfGuesses = false;


            while (Guess != SecretWord && !OutOfGuesses)             
            {
                if (GuessCount < GuessLimit)
                {
                    Console.Write("Enter a Guess: ");
                    Guess = Console.ReadLine();
                    GuessCount++;

                } else

                {
                    OutOfGuesses = true;
                }

                if (Guess != SecretWord)
                {
                    Console.WriteLine("Wrong Guess");

                }



            }  

              if (OutOfGuesses)
            {

                Console.WriteLine("You Lose");

            }   else {

                Console.WriteLine("You Win!");

            }               




            Console.ReadLine();

        }

    }
}

輸出是這樣的:

輸入一個猜測:dsadasd

猜錯

輸入一個猜測:dsdasd

猜錯

輸入一個猜測:dasdas

猜錯

輸入一個猜測:dasdas

猜錯

猜錯

你輸了

我不明白為什么最后我會得到雙重“錯誤猜測” 有誰知道為什么?

最后一次...

  1. guess仍然不等於secretword
  2. OutOfGuesses是 True
  3. GuessCount < GuessLimit為真

因此,您沒有提出問題,而是將 OutOfGuesses 設置為 True,但是由於guesssecretword不同, secretword您再次打印消息。

要解決此問題,您需要在if/else塊之外將OutOfGuesses設置為 true。 檢查它們是否已達到極限,並在循環結束時將其設置為 True。

即像...

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


namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {

            string SecretWord = "Banana";
            string Guess = "";
            int GuessCount = 0;
            int GuessLimit = 4;
            bool OutOfGuesses = false;


            while (Guess != SecretWord && !OutOfGuesses)             
            {
                Console.Write("Enter a Guess: ");
                Guess = Console.ReadLine();
                GuessCount++;

                if (GuessCount >= GuessLimit)
                {
                    OutOfGuesses = true;
                }

                if (Guess != SecretWord)
                {
                    Console.WriteLine("Wrong Guess");

                }

            }  

            if (OutOfGuesses)
            {
                Console.WriteLine("You Lose");

            } 
            else 
            {
                Console.WriteLine("You Win!");
            }                

            Console.ReadLine();

        } 
    }
}

Dasdas 不是你的秘密詞,所以 if 語句是真的,如果你插入 Banana 你的 if 語句將是假的

暫無
暫無

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

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