可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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(); }
回答1:
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(); } } }
回答2:
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.
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.
回答3:
Try this, Ask user to enter a number of dice store it in a variable then create an array of that size.