简体   繁体   English

如何让用户从控制台输入任意数量的变量

[英]How to let the user enter any amount of variable from Console

This is the code that I have made that rolls two dice until a pair appear. 这是我制作的将两个骰子滚动到一对骰子出现的代码。 My question is, is there a way for the user to enter any amount of dice he/she wants? 我的问题是,用户是否可以输入他/她想要的任何数量的骰子?

I don't want to create 50 int dice. 我不想创建50个int骰子。 If I use an array or List I would have the same problem. 如果我使用数组或列表,我将遇到同样的问题。 I'd have to assign each array section to numbergen 50 or more times. 我必须将每个数组部分分配给numbergen 50次或更多次。 Maybe there is something I am missing? 也许我缺少什么?

static void Main(string[] args)
        {

            Random numbergen = new Random();
            int dice1=0;
            int dice2=1;
            for (int counter = 0; counter <= 1; counter++)
            {
                while (dice1 != dice2)
                {
                    dice1 = numbergen.Next(1, 7);
                    dice2 = numbergen.Next(1, 7);
                    if (dice1 == dice2)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(dice1 + "\t" + dice2);
                        counter++;
                        dice1 = 0;
                        dice2 = 1;


                    }
                    else if (dice1 != dice2)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(dice1 + "\t" + dice2);
                    }
                    if (counter ==1 )
                    {
                        break;
                    }
                }
                }
            Console.ReadLine();

        }

If you want to store a number of integers specified by the user, then the easiest way to do that would be using an array like int x[z] where z is the number specified by the user, or, even better, make a list of ints in the form List<int> where you can add ints depending on the number the user entered. 如果要存储用户指定的整数,那么最简单的方法是使用类似int x[z]的数组,其中z是用户指定的数字,或者甚至列出一个列表形式为List<int>整数,您可以在其中根据用户输入的数字添加整数。

You wouldn't have the same problem as if you had many different variables when doing numbergen, because you could just have a for loop go through your list or array, giving them a value assigned by numbergen. 当您执行numbergen时,您不会有一个问题,就好像您有许多不同的变量一样,因为您可能只需要一个for循环遍历列表或数组,为它们提供由numbergen分配的值。

尝试此操作,请用户输入一些骰子,将其存储在变量中,然后创建该大小的数组。

Here's a version where all die must match. 这是所有骰子必须匹配的版本。

using System;

namespace Dicey
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOfDice;

            // check for and retrieve the number of dice the user wants.
            if (args.Length != 1 || !int.TryParse(args[0], out numberOfDice))
            {
                Console.WriteLine("Please provide the number of dice.");
                return; // exit because arg not provided
            }

            // Must have at least one and set some arbitrary upper limit
            if (numberOfDice < 1 || numberOfDice > 50)
            {
                Console.WriteLine("Please provide a number of dice between 1 and 50");
                return; // exist because not in valid range
            }

            var dice = new int[numberOfDice]; // create array of die (ints)
            var rand = new Random();
            var match = false; // start with false (match not found yet)

            while (!match) // loop until match becomes true
            {
                var message = string.Empty;
                match = true; // assume match until something doesn't match

                for (var i = 0; i < numberOfDice; i++)
                {
                    // initialize dice at index i with the random number
                    dice[i] = rand.Next(1, 7);

                    // build the message line to write to the console
                    message += dice[i]; // first add the die's number

                    if (i < numberOfDice - 1)
                        message += "\t"; // if not at the end, add a tab

                    // check if the previous die (except for the first of course) has a different number
                    if (i > 0 && dice[i - 1] != dice[i]) 
                        match = false; // uh oh, not all match. we have to keep going
                }

                // print out the message
                Console.ForegroundColor = match ? ConsoleColor.Yellow : ConsoleColor.White;
                Console.WriteLine(message);
            }

            Console.ReadLine();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让用户手动输入主键 - How to let the user to enter primary keys manually 如何不让任何鼠标/光标进入WPF中的窗口 - How to not let any mouse/cursor to enter the window in WPF 如何从最高到最低排序一个数组,并允许用户输入数组的一部分,而不是全部输入? - how to sort an array from highest to lowest and allowing the user to enter in part of the array, not the full amount alotted? 如何让用户输入整数并基于该值启动计时器 - How to let user enter an integer and start timer based off on that value 如何显示文本然后让用户编辑它? (控制台应用) - How can I display a text and then let the user edit it? (console application) 如何使用文件上传控件让用户从任何位置选择图像以保存到数据库中 - How to let the user select an image from any location using fileupload control for saving into databse 如何让用户从本地系统硬盘驱动器中的任何位置选择JPG文件 - How to let user select a JPG file from any location in the local system hard drive 我怎么能放一个规则,只允许您在C#的控制台应用程序中输入数字 - how can i put a rule that will only let you enter a number in a console application on C# c#-控制台应用程序-解决用户按任意数字输入时的错误 - c# - Console App - Solving error when user presses enter without any numbers 我如何让用户在 C# 中按他们希望的任何顺序选择 5 个项目? - How can i let a user choose between, let's say 5 items in any order they wish in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM