用户滚动两个骰子,应用程序告诉他们的运气取决于他们在两个骰子上获得相同数字的速度



运行此应用程序时。在我得到两个相同的号码后,我没有收到消息";祝你下次好运";。你能告诉我怎么了吗?

using System;

namespace FirstCode
{
class Program
{
static void Main(string[] args)
{
Random numberGen1 = new Random();
Random numberGen2 = new Random();
int roll1 = 5;
int roll2 = 0;
int count = 0;
Console.WriteLine("This is the LUCK indicator n Press ENTER to roll die and TEST LUCK.");

while (roll1 != roll2)
{
Console.ReadKey();
roll1=numberGen1.Next(1,7);
roll2=numberGen2.Next(1,7);
Console.WriteLine("Roll 1-- " + roll1);
Console.WriteLine("Roll 2-- " + roll2);
count++;
}

Console.WriteLine("It took you " + count + " attempts to roll same number on both die.");
count = 9;
Convert.ToInt32(count);
if(count == 1){
Console.WriteLine("You are extremely lucky");
}
else if (count == 2){
Console.WriteLine("You are LUCKY today");
}
else if(count >= 3){
if(count <= 5){
Console.WriteLine("Your LUCK is average");
}
}
else {
Console.WriteLine("Better LUCK next time");
}

Console.ReadKey();
}               


}
}

您把if/else语句搞砸了。如果你想让它更清晰(至少有一点(,也许你可以使用一个函数,它会根据计数返回正确的消息:

private string GetMessageFromCount(int count)
{
if (count <= 0)
throw new ArgumentException("Count cannot be 0 or negative", "count");
if (count == 1)
return "You are extremely lucky";
if (count == 2)
return "You are LUCKY today";
if (count <= 5) //we are obviously between 3 and 5 attempts
return "Your LUCK is average";
return "Better LUCK next time";
}

然后,为了表明人们有多幸运,只需调用方法:

Console.Writeline(GetMessageFromCount(count));

相关内容

最新更新