如何让用户从控制台上输入变量的任何金额 [英] How to let the user enter any amount of variable from Console

查看:99
本文介绍了如何让用户从控制台上输入变量的任何金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我做了,直到一对出现的滚动两个骰子的代码。
我的问题是,有没有对用户输入的骰子任何金额他/她想要?



我不希望创建50 INT方式骰子。如果我使用一个数组或列表我有同样的问题。我不得不为每个阵列部分numbergen 50次以上。也许有我丢失的东西?



 静态无效的主要(字串[] args)
{

随机numbergen =新的随机();
INT dice1 = 0;
INT dice2 = 1;
为(INT计数器= 0;反< = 1;计数器++)
{
,而(!dice1 = dice2)
{
dice1 = numbergen.Next( 1,7);
dice2 = numbergen.Next(1,7);
如果(dice1 == dice2)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(dice1 +\t+ dice2);
计数器++;
dice1 = 0;
dice2 = 1;


}
,否则如果(dice1 = dice2!)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(dice1 +\t+ dice2);
}
如果(反== 1)
{
中断;
}
}
}
到Console.ReadLine();

}


解决方案

下面是一个。版本,所有模具必须匹配

 使用系统; 

命名空间戴雪
{
类节目
{
静态无效的主要(字串[] args)
{
INT numberOfDice ;

//检查并检索用户想要骰子的数量。
如果(args.Length!= 1 ||!int.TryParse(参数[0],出numberOfDice))
{
Console.WriteLine(请提供骰子的数量。 );
的回报; //退出,因为阿根廷没有提供
}

//必须至少有一个,并设置一些任意的上限
如果(numberOfDice< 1 || numberOfDice→50)
{
Console.WriteLine(请提供一个数字1和50之间骰子的);
的回报; //存在,因为不是在有效范围内
}

变种骰子=新的INT [numberOfDice] //创建模具阵列(整型)
变种兰特=新的随机();
VAR匹配= FALSE; //使用虚假的开始(匹配尚未发现)

,而//循环,直到比赛成为真正的
{
VAR消息=的String.Empty(比赛!);
匹配= TRUE; //假设匹配,直到东西不匹配

(VAR I = 0; I< numberOfDice;我++)
{
//索引与初始化骰子我随机数
骰子由[i] = rand.Next(1,7);

//建行的消息写入到控制台
+消息=骰子[I] //先加模的数量

如果(I< numberOfDice - 1)
+消息=\t; //如果不是在末尾添加标签

//检查以前的模具(除第一期培训班的)具有不同数量的
如果(我大于0&安培; &安培;!骰子[我 - 1] =骰子[I])
匹配= FALSE; //嗯哦,不是所有的比赛。我们必须继续前进
}

//打印出消息
Console.ForegroundColor =匹配? ConsoleColor.Yellow:ConsoleColor.White;
Console.WriteLine(消息);
}

到Console.ReadLine();
}
}
}


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. 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. 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();

        }

解决方案

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();
        }
    }
}

这篇关于如何让用户从控制台上输入变量的任何金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆