[英]C# Do While looping problems
我是编码的新手,而我真的对这种C#do while循环业务很执迷。 经过很长时间的弄乱后,我终于让它循环了一条语句,但是现在它只重复第一个创建的语句。 例如,如果它生成数字6,而您猜到7,它将说“太高...”,但是当您再次猜测自己是太低还是等于6时,它仍然会说“太高”。 “。
谢谢
static void Main(string[] args)
{
Random r = new Random();
int intRandomNum;
// ask the random num gen for num between 1 and 12
intRandomNum = r.Next(10) + 1;
//ask user for their first guess
Console.WriteLine("I'm going to generate a random number, can you guess what it is? " + intRandomNum);
int intUserNum = int.Parse(Console.ReadLine());
do
{
if (intUserNum == intRandomNum)
{
Console.WriteLine("You got it! Great job!");
}
if (intUserNum < intRandomNum)
{
Console.WriteLine("Too low! Try Again.");
Console.ReadLine();
}
if (intUserNum > intRandomNum)
{
Console.WriteLine("Too high! Try again.");
Console.ReadLine();
}
} while (intUserNum != intRandomNum);
Console.ReadKey();
}
}
}
谢谢大家的帮助! 我知道了! 最后!
您需要将解析放入循环中,以便重新计算intUserNum
。
int intUserNum = int.Parse(Console.ReadLine());
do
{
if (intUserNum == intRandomNum)
{
Console.WriteLine("You got it! Great job!");
}
if (intUserNum < intRandomNum)
{
Console.WriteLine("Too low! Try Again.");
intUserNum = int.Parse(Console.ReadLine());
}
if (intUserNum > intRandomNum)
{
Console.WriteLine("Too high! Try again.");
intUserNum = int.Parse(Console.ReadLine());
}
} while (intUserNum != intRandomNum);
正如您所写的那样,您的代码会从用户那里读取新行,但是它永远不会将其转换为数字,因此您的变量不会更改。 这将使您的第一个错误猜测永远被使用。
注意,使用int.TryParse
而不是int.Parse
可能也是一个好主意。 如果用户键入的不是整数,这可以让您处理...
在循环中移动输入读数:
int intUserNum;
do
{
intUserNum = int.Parse(Console.ReadLine());
if (intUserNum == intRandomNum)
{
Console.WriteLine("You got it! Great job!");
}
if (intUserNum < intRandomNum)
{
Console.WriteLine("Too low! Try Again.");
}
if (intUserNum > intRandomNum)
{
Console.WriteLine("Too high! Try again.");
}
} while (intUserNum != intRandomNum);
那是因为代码实际上从用户intUserNum
获取一个数字到intUserNum
,即
int intUserNum = int.Parse(Console.ReadLine());
在循环之外 ,因此只会执行一次。 每次循环重复时,都会检查相同的旧数字。
编码提示:请记住,计算机不知道您打算做什么。 您的意图可能是正确的,即intUserNum
应该始终包含用户猜测的下一个数字。 但是,计算机只执行您告诉它的操作,并且您告诉用户用户只能输入一个数字-在循环内,您永远不会说intUserNum
应该采用新值,因此它将保留其旧值。永远。
我自己也遇到了类似的问题,并且在为整个课程进行了几乎两堂课前都在苦苦挣扎,直到我在(很讽刺地)输入自己的问题的过程中找到答案之前。
{
class Program
{
static void Main(string[] args)
{
string aKnight = "Romulus";
string bKnight = "Remus";
string Weapon;
Console.WriteLine($"{aKnight} implies {bKnight} is unorthodox.");
/*Console.ReadLine();*/
Console.WriteLine($"{bKnight} deicdes to hit {aKnight}. What should he use? Club, Bow, or Tactical Nuke?");
/*Console.ReadLine();*/
do
{
Weapon = Console.ReadLine();
switch (Weapon)
{
case "Club":
Console.WriteLine("Works for me.");
Console.WriteLine($"{bKnight} knocks {aKnight} out with the club.");
Console.ReadLine();
break;
case "Bow":
Console.WriteLine("Eh, creative yet acceptable.");
Console.WriteLine($"{bKnight} hits {aKnight} over the head with the bow. It just hurt a little.");
Console.ReadLine();
break;
case "Tactical Nuke":
Console.WriteLine("Are.. Are you serious? For the love of... JUST WHY?!");
Console.WriteLine("TACTICAL NUKE INBOUND!!!... It lagged...");
Console.ReadLine();
break;
default:
Console.Clear();
Console.WriteLine("Please make a choice.");
//Console.ReadLine();
break;
}
} while (Weapon != default && Weapon != "Club" && Weapon != "Bow" && Weapon != "Tactical Nuke");
Console.WriteLine("What will happen next ?");
Console.ReadLine();
}
}
}
我需要做的就是将Weapon = Console.ReadLine()移至do / while括号中,然后将(&& Weapon!=“ Club” && Weapon!=“ Bow” && Weapon!=“ Tactical Nuke”)添加到括号中。 非常感谢! :d
int intUserNum = int.Parse(Console.ReadLine());
应该在循环的顶部。
不知道C#,但是应该这样做。
您必须在循环内部获取并解析输入。 否则,该值始终保持不变:
// ...
do
{
int intUserNum = int.Parse(Console.ReadLine());
if (intUserNum == intRandomNum)
Console.WriteLine("You got it! Great job!");
if (intUserNum < intRandomNum)
{
Console.WriteLine("Too low! Try Again.");
Console.ReadLine();
}
if (intUserNum > intRandomNum)
{
Console.WriteLine("Too high! Try again.");
Console.ReadLine();
}
} while (intUserNum != intRandomNum);
static void Main(string[] args)
{
var rnd = new Random();
var n = rnd.Next(13);
while (!Guess(n)) ;
Console.ReadKey();
}
static bool Guess(int n)
{
int input;
if (!int.TryParse(Console.ReadLine(), out input))
return false;
var msg = input == n ? "Win" : input < n ? "Low" : "High";
Console.WriteLine(msg);
return input == n;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.