在C#中重复程序

时间:2014-03-05 23:13:59

标签: c#

我在C#中创建了一个骰子模拟器程序,但在分配要求中,它说它需要能够重复用户想要的次数。

在python中很简单:

def var():

    import random

    dicesides = int(input("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "))
    script(dicesides, random)

def script(dicesides, random):  
    if dicesides in (4,6,12):
        dice = int(random.randrange(1, dicesides))
        print(dicesides, " sided dice, score", dice)
    else:
        print("That number is invalid. Please try again.")
        var()

    repeat = str(input("Repeat? Simply put yes or no : "))

    if repeat == "yes":
        var()
    else:
        quit()            
var()

这是我的c#代码:

static void Main(string[] args)
        {
            Random random = new Random();

            int num;
            Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: ");
            num = Convert.ToInt32(Console.ReadLine());

            if (num == 4 || num == 6 || num == 12)
            {
                int randomDice = random.Next(0, num);
                Console.WriteLine(num + " sided dice thrown, " + randomDice);
            }
            else
            {
                Console.WriteLine("That number is invalid. Please try again.")
            }


        }

如何让程序重复?

2 个答案:

答案 0 :(得分:4)

while(true)break

一起使用
    Random random = new Random();
    while(true)
    {
        int num;
        Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: ");
        num = Convert.ToInt32(Console.ReadLine());

        if (num == 4 || num == 6 || num == 12)
        {
            int randomDice = random.Next(0, num);
            Console.WriteLine(num + " sided dice thrown, " + randomDice);
        }
        else
        {
            Console.WriteLine("That number is invalid. Please try again.")
        }
        Console.WriteLine("Try again? (yes/no)");
        if(Console.ReadLine().ToLower() != "yes")
             break;   // end the while loop
    }

请注意,更简洁的方法是将循环的内容放在一个单独的函数中并从循环中调用它:

static Random random = new Random();   // make the Random a field of the class for reuse
public static void Main()
    while(true)
    {
        RollDie();
        Console.WriteLine("Try again? (yes/no)");
        if(Console.ReadLine().ToLower() != "yes")
             break;   // end the while loop
    }
}
public static void RollDie()
{
    int num;
    while(true)
        Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: ");
        num = Convert.ToInt32(Console.ReadLine());

        if (num == 4 || num == 6 || num == 12)
        {
            int randomDice = random.Next(0, num);
            Console.WriteLine(num + " sided dice thrown, " + randomDice);
            break; // exit the while loop
        }
        else
        {
            Console.WriteLine("That number is invalid. Please try again.")
        }
    }
}

答案 1 :(得分:1)

您必须将代码置于while循环中,如下所示:

static void Main(string[] args)
    {
        Console.WriteLine("Do you want to roll the dice ? Y/N");
        char c = Convert.ToChar(Console.ReadLine());
        while(c.ToLower() == 'y')
        {
        Random random = new Random();

        int num;
        Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: ");
        num = Convert.ToInt32(Console.ReadLine());

        if (num == 4 || num == 6 || num == 12)
        {
            int randomDice = random.Next(0, num);
            Console.WriteLine(num + " sided dice thrown, " + randomDice);
        }
        else
        {
            Console.WriteLine("That number is invalid. Please try again.")
        }
        Console.WriteLine("Do you want to roll it again? Y/N");
        c = Convert.ToChar(Console.ReadLine());
      }

    }

希望它有所帮助...