C#逻辑问题使用相同的输入进入错误的循环



是一个简单的c#猜谜游戏,在这个游戏中,类封装了一个随机数,驱动程序随机选择一个数字来猜测它太高或太低,问题是当它达到更高时(我用来检查这个猜测数是否高于目标数),如果你看到我的目标是10,它会给我错误的消息,并且猜测数是11,该猜测>target应该一直转到(db[i].rehigher)this循环,但它在到达正确路径后转到else循环。我不知道那是怎么回事。```status=true;reseted=false;更高=错误;//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

为什么要使用这么多代码来检查数字是否过高?更干净更容易的代码

PD_3

最新更新