c#猜谜游戏,只有X次尝试
本文关键字:只有 游戏 | 更新日期: 2023-09-27 18:15:40
这基本上是我的第一个小c#程序。我建立了一个"规则",所以你只有20次尝试猜测正确的数字。我想知道是否有一种更漂亮的方式来实现这个"规则"。
这是我的代码:
bool run = true;
while (run == true) // Main While Schleife
{
Random rndnr = new Random();
int rnd;
rnd = rndnr.Next(1, 100);
int guess;
int trys;
trys = 0;
guess = 0;
Console.WriteLine("Can you guess my number? I am thinking about a number between 1 and 100!");
guess = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(rnd);
while (guess != rnd)
{
if (trys > 20)
{
Console.Clear();
Console.WriteLine("You only have 20 trys. You failed! 'n");
run = false;
Console.WriteLine("Do you wann play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (rerun == "R")
{
run = true;
trys = 0;
Console.Clear();
break;
}
else
{
run = false;
break;
}
}
if (guess > rnd)
{
Console.WriteLine("My number is lower than" + guess + "'n guess again >>");
guess = Convert.ToInt32(Console.ReadLine());
}
if (guess < rnd)
{
Console.WriteLine("My number is higher than" + guess + "'n guess again >>");
guess = Convert.ToInt32(Console.ReadLine());
}
if (guess == rnd)
{
Console.Write("You guessed right my number is" + rnd + "and you needed " + trys + "trys");
run = false;
Console.WriteLine("Do you wann play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (rerun == "R")
{
run = true;
trys = 0;
Console.Clear();
}
else
{
run = false;
break;
}
}
trys++;
}
}
Console.Clear();
Console.WriteLine("Thanks for palying see you next time!");
Console.ReadLine();
}
}
我修复了一些错误,并尝试改进代码。希望对你有帮助:
bool run = true;
int maxTrys=20;
Random random = new Random();
while (run)
{
int rnd=random.Next(1, 101);
Console.WriteLine("Can you guess my number? I am thinking about a number between 1 and 100!");
int trys=0;
bool solved=false;
while (!solved && trys<maxTrys)
{
trys++;
int guess;
bool numericGuess;
do {
numericGuess=int.TryParse(Console.ReadLine(), out guess);
if (!numericGuess) {
Console.WriteLine("Please enter a number");
}
} while (!numericGuess);
if (guess > rnd)
{
Console.WriteLine(string.Format("My number is lower than {0}'n guess again >>", guess));
}
else if (guess < rnd)
{
Console.WriteLine(string.Format("My number is higher than {0}'n guess again >>", guess));
}
else if (guess == rnd)
{
solved=true;
Console.WriteLine(string.Format("You guessed right my number is {0} and you needed {1} trys", rnd, trys));
}
}
if (!solved) {
Console.Clear();
Console.WriteLine(string.Format("You only have {0} trys. You failed! 'n", maxTrys));
}
Console.WriteLine("Do you wanna play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (!rerun.Equals("R"))
{
run = false;
}
}
Console.Clear();
Console.WriteLine("Thanks for playing see you next time!");
Console.ReadLine();
Random r = new Random();
int secretNumber = r.Next(1, 11);
int i = 0;
int chance = 5;
Console.WriteLine("Game: guess number");
while (i < chance)
{
Console.WriteLine("Your turn");
if (secretNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("You won!");
break;
}
i++;
}
if (i == chance)
{
Console.WriteLine("You lost!");
Console.WriteLine("The secret number is " + secretNumber);
}
简单方法