【发布时间】: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