我应该来自字符串txt" jvn" ,只获得一个随机字符,但由于某种原因它不会工作我也尝试循环后再将我的字符转换为字符串,但它没有工作,我没有返回值
static char Zufallszeichen(string s)
{
Random rnd = new Random();
string x = "jvn";
string result = "";
Convert.ToChar(x);
for (int i = 0; i < 1; i++)
{
result += x.Substring(rnd.Next(0, x.Length), 1);
}
return x;
}
答案 0 :(得分:1)
我假设你想从输入字符串中获取一个随机字符,对吗?
首先要做的事情: 你似乎对C#或一般的编程都很新。也许你想要一些tutorials。或者你可以拿一本好的编程书。
尽管如此,让我们来看看:
export class SourceSystems implements BaseEntity {
constructor(
public id?: number,
public name?: string,
public identifier?: string,
public currencies?: Array<Currency>,
public lendingActivated?: boolean,
public walletApiActivated?: boolean,
) {
this.lendingActivated = false;
}
}
这是一个非常简单的方法:
static char Zufallszeichen(string s) /* You never use the argument, why do you have one* */
{
Random rnd = new Random();
string x = "jvn"; // You are creating this string and returning it unchanged at the end
string result = "";
Convert.ToChar(x); // This ->returns<- the input as char... so basicly you have to catch the value. But why would you that in the first place
for (int i = 0; i < 1; i++) // You're looping for 1 iteration (i.e. running the code inside once)
{
result += x.Substring(rnd.Next(0, x.Length), 1); // You're appending a random character to the result _string_ but never returning it.
}
return x; // You will only return jvn, as x has remained unchanged.
}
编辑:我知道有更简单或更简单的答案,但我希望这种方法更容易理解&#34;。
答案 1 :(得分:0)
Followig代码为我工作:
static char randomLetter(string s)
{
Random rnd = new Random();
int index = rnd.Next (0, s.Length);
return s[index];
}
char leter = randomLetter ("abcdef");