[英]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.