这基本上是我的第一个小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();
}
}
答案 0 :(得分:0)
我做了一些错误修复,并且通常尝试改进代码。希望你觉得它有用:
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();
答案 1 :(得分:-2)
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);
}
简单方法