Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取字符';随机生成的位置,该位置作为字符串存储在字符数组中_C# - Fatal编程技术网

C# 获取字符';随机生成的位置,该位置作为字符串存储在字符数组中

C# 获取字符';随机生成的位置,该位置作为字符串存储在字符数组中,c#,C#,我创建了一个程序,可以从存储在字符数据类型数组中的给定单词中随机生成一个字母 例如: 生成并显示strong和r 如何获取r的位置并显示它 s-1,t-2,r-3,o-4,n-5,g-6。字母r是第三个字母 由于我已将单词存储在字符数组中,默认情况下,数组的索引值从0开始,我无法重置它 我已经生成了r,如何才能在不篡改我的角色数组的情况下获取并显示它的位置 还有什么地方可以比较随机生成的r和它的位置吗?是你的朋友: int index = Array.IndexOf(characters, ra

我创建了一个程序,可以从存储在字符数据类型数组中的给定单词中随机生成一个字母

例如:

生成并显示strong和r

如何获取r的位置并显示它

s-1,t-2,r-3,o-4,n-5,g-6。字母r是第三个字母

由于我已将单词存储在字符数组中,默认情况下,数组的索引值从0开始,我无法重置它

我已经生成了r,如何才能在不篡改我的角色数组的情况下获取并显示它的位置

还有什么地方可以比较随机生成的r和它的位置吗?

是你的朋友:

int index = Array.IndexOf(characters, randomChar) + 1;
//+1 at the end because indexes are zero-based

既然您不提供任何代码,那么让我来演示如何实现它:

public static string RandomChar(string s) {
    Random r = new Random();
    int i = r.Next(s.Length);
    return s[i] + " - " + (i+1);
}
这会随机选取索引并返回该索引处的字符以及该字符基于1的索引,例如
“r-3”

称之为:

string result = RandomChar("strong");
// Do something with the result, e.g. Console.WriteLine(result).

您可以按如下方式使用
Array.IndexOf

var word = new[] { 's', 't', 'r', 'o', 'n', 'g' };
var character = 'r';

var position = Array.IndexOf(word, character);
var word = new[] { 's', 't', 'r', 'o', 'n', 'g', 'e', 'r' };
var character = 'r';

foreach (var position in word.AllIndexesOf(character))
{
    Console.WriteLine(position.ToString());
}
注意:由于数组的索引为零,因此需要在位置上添加1以获得
3
,因为在本例中IndexOf将返回
2

如果要显示给定角色的所有位置,则需要创建一个方法来查找它们(类似于此,但可能会有所改进):


你必须这样做吗?这是C#?因为您可以使用php执行此操作:

<?php
$mystring = 'strong';
$findme   = 'r';
$pos = strpos($mystring, $findme);
$posadj = $pos +1; //this will offset because the array starts at 0.

// must use ===
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos which when offset is really $pos1.";
}
?>

为什么不让你的程序在给定的单词中随机生成一个位置,然后取而代之的是该位置的字符呢?我的答案就是这样的,请参阅下面的内容。如果同一字符多次出现且所需索引不是第一次出现,则此操作将不起作用。如果同一字符多次出现且所需索引不是第一次出现,则此操作将不起作用。这是另一个问题,而不是OP要求的帮助。我不同意。OP写的是“例如”,所以他可能使用了单词“character”和字母“r”,然后“r”的索引将基于
数组。IndexOf
不明确。如果我遗漏了什么,请纠正我。
<?php
$mystring = 'strong';
$findme   = 'r';
$pos = strpos($mystring, $findme);
$posadj = $pos +1; //this will offset because the array starts at 0.

// must use ===
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos which when offset is really $pos1.";
}
?>