如何select 随机数数组的多个值
How can select multiple values of an array of random numbers
我正在创建一个骰子程序来进一步学习。该程序将掷出 5 个骰子并将随机数值分配给 5 的数组并打印出这些值。之后,我希望玩家 select 他或她想要保留的一个或多个骰子,显示这些值,稍后我将创建一个方法来掷剩余的骰子。
我在 select 多个骰子部分遇到问题。
这是我目前拥有的:
using System;
class HelloWorld {
static void Main() {
Random random = new Random();
int[] diceEach = new int[5];
int diceCount = 1;
for(int i = 0; i < 5; i++)
{
diceEach[i] = random.Next(1, 7);
Console.WriteLine("Dice " + diceCount +": " + diceEach[i]);
diceCount++;
}
Console.WriteLine("To roll again hit R");
Console.WriteLine("Please type the dice numbers that you would like to keep...");
string diceKept = Console.ReadLine();
if(diceKept == "1")
{
Console.Write(diceEach[0]);
}
else if(diceKept == "1")
{
Console.WriteLine(diceEach[1]);
}
else if(diceKept == "3")
{
Console.WriteLine(diceEach[2]);
}
else if(diceKept == "4")
{
Console.WriteLine(diceEach[3]);
}
else if(diceKept == "5")
{
Console.WriteLine(diceEach[4]);
}
else if(diceKept == "r")
{
Console.WriteLine("You have chosen to roll again");
}
Console.ReadLine();
}
}
我目前打印出一个骰子值,您 select 但我不确定如何打印出多个 select 离子。我能想到的唯一方法是输入所有选项。但这似乎不对,会使我的代码变长。
我在想某种循环可能有用吗?但我知道怎么做。
这是我第一次在这里发帖,希望我做对了。
提前致谢!
您可以通过让用户输入他选择的骰子并用逗号“,”分隔它们来获得多个用户选择,例如
1,2,5
然后展开代码
string diceKept = Console.ReadLine();
try{
int[] selectedDices = diceKept.Split(',').Select(x => int.Parse(x)).ToArray();
}catch{
//invalid input - string value could not be parsed to int value.
}
您可以在函数中使用 try-catch 块,如果没有抛出错误,则继续您的工作,否则重试输入所选值。
您可以解决的另一种方法是循环,直到用户完成选择要保留的骰子并希望使用 do...while 循环重新掷骰子。
string diceKept;
do
{
Console.WriteLine("Please type the dice number that you would like to keep...and type 'r' to when finished to re-roll remaining dice");
diceKept = Console.ReadLine();
if(int.TryParse(diceKept, out var diceNumber))
{
// I am making an assumption here that the user will always input a number between 1-6.
// It would be best to put some logic to catch that scenario though.
Console.WriteLine(diceEach[diceNumber - 1]);
}
} while (diceKept.ToLower() != "r");
Console.WriteLine("Now re-rolling the rest....");
// Do some re-relling stuff here.
Console.Read()
我正在创建一个骰子程序来进一步学习。该程序将掷出 5 个骰子并将随机数值分配给 5 的数组并打印出这些值。之后,我希望玩家 select 他或她想要保留的一个或多个骰子,显示这些值,稍后我将创建一个方法来掷剩余的骰子。
我在 select 多个骰子部分遇到问题。
这是我目前拥有的:
using System;
class HelloWorld {
static void Main() {
Random random = new Random();
int[] diceEach = new int[5];
int diceCount = 1;
for(int i = 0; i < 5; i++)
{
diceEach[i] = random.Next(1, 7);
Console.WriteLine("Dice " + diceCount +": " + diceEach[i]);
diceCount++;
}
Console.WriteLine("To roll again hit R");
Console.WriteLine("Please type the dice numbers that you would like to keep...");
string diceKept = Console.ReadLine();
if(diceKept == "1")
{
Console.Write(diceEach[0]);
}
else if(diceKept == "1")
{
Console.WriteLine(diceEach[1]);
}
else if(diceKept == "3")
{
Console.WriteLine(diceEach[2]);
}
else if(diceKept == "4")
{
Console.WriteLine(diceEach[3]);
}
else if(diceKept == "5")
{
Console.WriteLine(diceEach[4]);
}
else if(diceKept == "r")
{
Console.WriteLine("You have chosen to roll again");
}
Console.ReadLine();
}
}
我目前打印出一个骰子值,您 select 但我不确定如何打印出多个 select 离子。我能想到的唯一方法是输入所有选项。但这似乎不对,会使我的代码变长。
我在想某种循环可能有用吗?但我知道怎么做。
这是我第一次在这里发帖,希望我做对了。 提前致谢!
您可以通过让用户输入他选择的骰子并用逗号“,”分隔它们来获得多个用户选择,例如
1,2,5
然后展开代码
string diceKept = Console.ReadLine();
try{
int[] selectedDices = diceKept.Split(',').Select(x => int.Parse(x)).ToArray();
}catch{
//invalid input - string value could not be parsed to int value.
}
您可以在函数中使用 try-catch 块,如果没有抛出错误,则继续您的工作,否则重试输入所选值。
您可以解决的另一种方法是循环,直到用户完成选择要保留的骰子并希望使用 do...while 循环重新掷骰子。
string diceKept;
do
{
Console.WriteLine("Please type the dice number that you would like to keep...and type 'r' to when finished to re-roll remaining dice");
diceKept = Console.ReadLine();
if(int.TryParse(diceKept, out var diceNumber))
{
// I am making an assumption here that the user will always input a number between 1-6.
// It would be best to put some logic to catch that scenario though.
Console.WriteLine(diceEach[diceNumber - 1]);
}
} while (diceKept.ToLower() != "r");
Console.WriteLine("Now re-rolling the rest....");
// Do some re-relling stuff here.
Console.Read()