gpt4 book ai didi

c# - 在控制台打印数组

转载 作者:太空狗 更新时间:2023-10-30 01:28:56 27 4
gpt4 key购买 nike

我想要一个返回数组的函数。我首先使用 void 函数对此进行了测试,但控制台中的字符串为空,而且我没有找到我的保管库。

private void getValueOfRadio() {
string[,] arrayUrl = new String[4, 3] {
{ "xx0", "xxx0", "xxxx0" },
{ "xx1", "xxx1", "xxxx1" },
{ "xx2", "xxx2", "xxxx2" },
{ "xx3", "xxx3", "xxxx3" }
};

var checkedRadioButton = groupUrl
.Controls
.OfType<RadioButton>()
.FirstOrDefault(x => x.Checked == true);

int i = 0;
if (checkedRadioButton != null) {
switch (checkedRadioButton.Text) {
case "MK-Live":
i = 1;
break;
case "MK-Test":
i = 2;
break;
case "Roland Test":
i = 3;
break;
default:
i = 0;
break;
}
}

string[] returnArray = new string[] {
arrayUrl[i, 0], arrayUrl[i, 1], arrayUrl[i, 2] };

Console.WriteLine(returnArray);
}

最佳答案

让我们提取一些方法:RowIndexFromButton - 我们要打印哪一行(基于单选按钮)和RowFromArray - 从二维数组中提取行.

   private int RowIndexFromButton() {
var checkedRadioButton = groupUrl
.Controls
.OfType<RadioButton>()
.FirstOrDefault(x => x.Checked);

if (checkedRadioButton == null)
return -1; //TODO: or 0 if we want to get 0th record

switch (checkedRadioButton.Text) {
case "MK-Live":
return 1;
case "MK-Test":
return 2;
case "Roland Test":
return 3;
default:
return 0;
}
}

private static IEnumerable<T> RowFromArray<T>(T[,] array, int row) {
if (null == array)
throw new ArgumentNullException(nameof(array));
else if (row < array.GetLowerBound(0) || row > array.GetUpperBound(0))
yield break;

for (int i = array.GetLowerBound(1); i <= array.GetUpperBound(1); ++i)
yield return array[row, i];
}

然后我们可以轻松地将这两种方法结合在一起:

I wanna have a function which return a array

   private T[] RowFromButton<T>(T[,] array) {
return RowFromArray(array, RowIndexFromButton()).ToArray();
}

并使用它:

   string[,] arrayUrl = new String[4, 3] {
{ "xx0", "xxx0", "xxxx0" },
{ "xx1", "xxx1", "xxxx1" },
{ "xx2", "xxx2", "xxxx2" },
{ "xx3", "xxx3", "xxxx3" }
};

string[] returnArray = RowFromButton(arrayUrl);

// When printing collection (array) we should join items (e.g. with space)
Console.WriteLine(string.Join(" ", returnArray));

关于c# - 在控制台打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com