【问题标题】:C# logic problem get to the wrong loop with the same inputC# 逻辑问题使用相同的输入进入错误的循环
【发布时间】:2021-09-29 20:46:39
【问题描述】:

是一个简单的c#猜谜游戏,类封装了一个随机数,驱动程序随机一个数来猜测它太高或太低,问题是驱动程序当涉及到Higher时(这个我曾经检查过猜测数字是否高于目标数字)如果你看到我的核心目标是10,它会给我错误的信息,猜测数字是11,猜测>目标然后应该去(db [i].higher)这个循环全部时间,但它在到达正确的路径后进入了 else 循环。我不知道那有什么问题。 ``` 状态=真; 重置=假; 更高=假; //lower = false;

    }
    public bool Query(int guess)
    {

        if (!status|| guess < 0) return false;
        total_round++;

        if (guess > num )
        {
            higher_count++;
            higher = !higher;
            return false;
        }
        if (guess < num)
        {
            lower_count++;
            //!Compare;
            //higher = false;
            Console.WriteLine("get here!\n");
            return false;
        }
        else
        {
            right_count++;
            status = !status;
            return true;
        }
        //return false;
    }

    public bool Higher
    {
        get { return higher;}
    }
``` int input= rnd.Next(5, 10);
        //int round = rnd.Next(5,10);
        int round = 2;
        targetInt[] db = new targetInt[2];
        for (int i = 0; i < db.Length; i++)
        {
            Console.WriteLine("==================New Round Of 
          Game=================");
            db[i] = new targetInt(i);
            int counter = 1;
            while (counter <= round)
            {
                int bound = 0, upperbound = 100;
                //int guess = rnd.Next(bound, upperbound);
                int guess = 11;//guess < target
                //int guess = i;
                Console.WriteLine("Guess number is: " + guess);
                //list[i] = guess;

                if (db[i].Query(guess))
                {
                    Console.WriteLine("Round : " + db[i].TotalRound + " Guess number 
           is: " + guess + "\n");
                    Console.WriteLine("You are right!\n");
                    break;
                }
                else
                {
                    if (!db[i].Status)
                    {
                        Console.WriteLine("You dont have any chance left!");
                        break;
                    }

                    
                    if (db[i].Higher)//guess > num                       
                    {
                        Console.WriteLine(db[i].Higher.ToString());
                        Console.WriteLine("Guess number is too big, guess again");
                        upperbound = guess - 2;
                        Console.WriteLine("Round : " + db[i].TotalRound + " Guess 
          number is: " + guess + "\n");
                        

                    }
                   // if(!db[i].Higher && db[i].Status)//guess < num
                    else
                    {
                        Console.WriteLine(db[i].Higher.ToString());
                        Console.WriteLine("Guess number is too small, guess again");
                        bound = guess + 2;
                        Console.WriteLine("Round : " + db[i].TotalRound + " Guess 
         number is: " + guess + "\n");   
                    }   
                }
                counter++;
            }

            //Console.WriteLine("You dont have any chance left!");
            Console.WriteLine("====================Game 
             Statics====================");
            Console.WriteLine("Total round: " + db[i].TotalRound);
            Console.WriteLine("Right guess count: " + db[i].RightCount);
            Console.WriteLine("Higher guess count: " + db[i].HigherCount);
            Console.WriteLine("lower guess count: " + db[i].LowerCount);
            Console.WriteLine("GAME OVER!!!!\n");
        }
```
>this is output message, the round two should be the same as round 1 since my guess 
```  number is hardcoded 11;
Target: 10
Guess number is: 11
True
Guess number is too big, guess again
 Round : 1 Guess number is: 11

Guess number is: 11
False
Guess number is too small, guess again
Round : 2 Guess number is: 11

【问题讨论】:

    标签: c# if-statement logic


    【解决方案1】:

    你为什么要使用这么多代码来检查数字是否太高? 更简洁的代码

    private static int limit = 10;
     // change this int to the number you consider high limit, so if the guess is more than that the number is considered high
    bool high;
    do {
       high = highNum(guess);
    } while (!high);
    public bool highNum (int guess)
    {
          if (guess > limit) {
              return true;
              }
          else {
              return false;
               }
    }
    

    【讨论】:

    • 我需要快速游戏例如,如果guess = target,我需要关闭游戏,但是如果guess > target,我需要随机一个较小的数字......等等,在这种情况下,我只是调试了我的驱动程序,发现它打印了错误的消息。
    • 你不需要随机一个较小的数字,只需使用guess &lt; limit
    • 重点不在于类的实现,我只是想知道为什么,在我的驱动程序中,每轮猜测数都是 11,但它总是走错路:
    • @hoanon wang 使用 do while 循环作为上面的代码,我认为都是因为那个查询。
    • 我修好了,我在构造函数中声明的默认是false,然后我在guess > target的时候写了然后higher = !更高,这给了我错误,我改为更高 = true,它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2012-02-24
    相关资源
    最近更新 更多