数组例题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 练习
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 输入一个字符串,输出每个单词,然后再用下划线链接输出。
            string str = textBox1.Text;
            string[] str1 = str.Split(' ');
            for (int i=0;i<str1.Length;i++) 
            {
                MessageBox.Show(str1[i]);
            }
            MessageBox.Show(str.Replace(" ","_"));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //输入一个字符串(每个单词以空格分隔开),将字符串按照驼峰命名法的规则连接后输出
            string str = textBox2.Text.Trim();//避免第一个字符串开始为空
            string[] str1 = str.Split(' ');//以空格将字符串隔开
            string typore = "";//生成新的变量接收
            foreach (var item in str1)//遍历出其中每一个以空格隔开后的字符
            {
                typore += item.Substring(0, 1).ToUpper() + item.Substring(1)+" ";//将其中每个独立的字符串首字母大写赋给typore
            }
            string type = typore.Substring(0, 1).ToLower() + typore.Substring(1);//将第一个独立的字符首字母变为小写拼接上为变小写的字符
            textBox3.Text += type;//输出
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //随机获取0 - 9之间6个数,并保证两两相同(1,2,3,2,2,1,3)
            Random r = new Random();
            int[] str = new int[3];
            int[] str1 = new int[6];
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = r.Next(0,10);
                for (int j = 0; j < i; j++)
                {
                    if (str[i]==str[j])
                    {
                        i--;
                    }
                }

            }
            for (int i = 0; i < str1.Length; i++)
            {
             //随机获取0 - 9之间6个数,并保证两两相同(1,2,3,2,2,1,3)
                int s = 0;
               // str1[i] = r.Next(0,str.Length);
                str1[i] = str[r.Next(0,str.Length)];
                for (int j = 0; j < i; j++)
                {
                    if (str1[i]==str1[j])
                    {
                        s++;
                        if (s>1)
                        {
                            i--;
                        }
                    }
                }
            }
            for (int i = 0; i < str1.Length; i++)
            {
                textBox4.Text += str1[i];
            }
        }
        //随机获取0-100之间的(n^2)个数,并保证数字是n个相同(1,4,5,5,1,4,4,5,1)
        private void button4_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            int n = r.Next(0,11);
            int[] str = new int[n];
            int[] str1 = new int[n*n];
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = r.Next(0,101);
                for (int j = 0; j < i; j++)
                {
                    if (str[i]==str[j])
                    {
                        i--;
                    }
                }
            }
            for (int i = 0; i < str1.Length; i++)
            {
                int s = 0;
                str1[i] = str[r.Next(0, str.Length)];

                for (int j = 0; j < i; j++)
                {
                    if (str1[i] == str1[j])
                    {
                        s++;
                        if (s > n - 1)
                        {
                            i--;
                        }
                    }
                }
            }
            for (int i = 0; i < str1.Length; i++)
            {
                listBox1.Items.Add(str1[i] + " ");
                textBox5.Text+=str1[i]+" ";
            }
           
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值