c# - 在 1-10 之间选择一个数字 游戏每轮后不能产生不同/新的数字
问题描述
我正在尝试开发一款应用程序/小游戏,该游戏旨在尝试猜测与程序相同的 1-10 之间的相同数字并获胜,但在那轮之后你应该能够再次玩!我的问题是程序每次都会生成相同的数字。我已经浏览了这里的其他线程,但它们似乎都没有帮助,或者至少我无法理解它们。我怎样才能解决这个问题?
static void Main(string[] args)
{
Random random = new Random();
bool play = true;
int playnum = random.Next(1, 11);
while (play)
{
Console.Write("\n\tGuess number between 1-10 ");
int numb;
bool success = Int32.TryParse(Console.ReadLine(), out numb);
if (success)
{
if (numb < playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tinput number " + numb + " too small, try again.");
}
if (numb > playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tinput number " + numb + " too big, try again.");
}
if (numb == playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tcongrats!");
bool LoopBreaker = true;
do
{
Console.WriteLine("continue or finish?");
string input = Console.ReadLine();
char CharFromTryPass = ' ';
bool TryparsResult = char.TryParse(input, out CharFromTryPass);
bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
if (IsValidChar)
{
Console.WriteLine(" please write A or F!");
}
else if (TryparsResult)
{
if (CharFromTryPass == 'A')
{
Console.WriteLine("see you!");
play = false;
LoopBreaker = false;
}
if (CharFromTryPass == 'F')
{
play = true;
LoopBreaker = false;
}
}
} while (LoopBreaker);
解决方案
你的代码有点乱,你应该把它分解成更小的方法。但基本上你只需要在人们猜对后生成一个新数字。所以在“恭喜!”之后添加这个。线。
playnum = random.Next(1, 11);
.
static void Main(string[] args)
{
Random random = new Random();
bool play = true;
int playnum = random.Next(1, 11);
while (play)
{
Console.Write("\n\tGuess number between 1-10 ");
int numb;
bool success = Int32.TryParse(Console.ReadLine(), out numb);
if (success)
{
if (numb < playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tinput number " + numb + " too small, try again.");
}
if (numb > playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tinput number " + numb + " too big, try again.");
}
if (numb == playnum)
{
Console.WriteLine(playnum);
Console.WriteLine("\tcongrats!");
playnum = random.Next(1, 11);
bool LoopBreaker = true;
do
{
Console.WriteLine("continue or finish?");
string input = Console.ReadLine();
char CharFromTryPass = ' ';
bool TryparsResult = char.TryParse(input, out CharFromTryPass);
bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
if (IsValidChar)
{
Console.WriteLine(" please write A or F!");
}
else if (TryparsResult)
{
if (CharFromTryPass == 'A')
{
Console.WriteLine("see you!");
play = false;
LoopBreaker = false;
}
if (CharFromTryPass == 'F')
{
play = true;
LoopBreaker = false;
}
}
} while (LoopBreaker);
}
}
}
}
推荐阅读
- php - 第一个关系的 Laravel 6.x 关系默认值
- jekyll - 如何解决 Jekyll-Include-Cache 问题?
- amazon-web-services - AWS s3 同步从 s3 复制到 EMR 很慢
- vb.net - VB.NET 列表视图委托
- node.js - 如何确定 NPM 包是实现 ES5 还是 ES6 模块?
- docker - dotnet restore 使用 docker Jenkins 容器失败
- jquery - 为什么这个 jquery 方法不返回 SELECTED 值?
- javascript - Node Js,多次调用导出的函数
- python-3.x - 如何在给定增长率和特殊规则的情况下获得 pandas 的累积增长?
- css - Ruby on Rails 6 -- 使用 Webpacker 和 Bootstrap 在局部视图上没有 CSS