如何生成可以包含加号或减号(- 或 +)的随机字符串 [英] How can i generate a random string that can contain either plus sign or minus sign (- or +)

查看:75
本文介绍了如何生成可以包含加号或减号(- 或 +)的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个数学游戏,我需要让它随机化 -+.
我已经试过了:

I'm making a math game, and I need to make it randomize either - or +.
I have already tried this:

Random rnd = new Random();
string name = rnd.Next(-, +);  

但这行不通,有没有人有办法做到这一点?注意:我是初学者

But that doeen't work, Anyone have a way I can do this? Note: Im a beginner

推荐答案

public static class Extensions
{
    public static T NextItem<T>(this Random r, params T[] items)
    {
        //  Thanks to nbokmans for catching my error here: The second parameter
        //  is not the largest value it can return; it's the "exclusive upper bound".
        return items[r.Next(0, items.Length)];
    }
}

...

Random rnd = new Random();
string name = rnd.NextItem("-", "+");

这篇关于如何生成可以包含加号或减号(- 或 +)的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆