随机数问题:
ArrayList arrayRandom=new ArrayList();
int index=0;
//通过Random产生的随机数是伪随机数,伪随机数产生需要一个种子。
//只要种子一样每次产生的随机数就都是一样的。
Random random=new Random();
while (arrayRandom.Count < 10)
{
// Random random = new Random();
int rd=random.Next(1,101);
index++;
if (rd % 2 == 0)
{
if (!arrayRandom.Contains(rd))
{
arrayRandom.Add(rd);
}
}
}
for (int i = 0; i < arrayRandom.Count; i++)
{
Console.WriteLine(arrayRandom[i]);
}
Console.WriteLine("一共循环了:{0}次",index);
Console.ReadKey();
泛型集合
//通过泛型可以限制集合中存放的数据的类型
List<int> list = new List<int>();
list.Add(10);
list.Add(20);
list.Add(20);
Console.WriteLine( list.Max());
Console.WriteLine(list.Min());
Console.WriteLine( list.Sum());
Console.WriteLine(list.Average());
List<string> strList = new List<string>();
strList.Add("helloworld!");
strList.Add("hello");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
//返回值就是整形数组
//int[] lists= list.ToArray();
Console.ReadKey();
1.分检奇偶:先输出基数再输出偶数
// 第一种方法:
// string ms = "2 7 9 3 6 5 8 4";
// string[] nums = ms.Split(' ');//对字符串进行分离
// ArrayList listOdd = new ArrayList();
// ArrayList listEven = new ArrayList();
// for (int i = 0; i < nums.Length;i++ )
// {
// if (Convert.ToInt32(nums[i]) % 2 != 0)
// {
// listOdd.Add(nums[i]);
// }
// else {
// listEven.Add(nums[i]);
// }
// }
// listOdd.AddRange(listEven);
// StringBuilder sb=new StringBuilder();
// for(int i=0;i<listOdd.Count;i++)
// {
// sb.Append(listOdd[i]+"");
// }
// Console.WriteLine(sb);
// Console.ReadKey();
第二种方法:
List<int> list = new List<int>();
list.Add(10);
list.Add(2);
list.Add(7);
list.Add(9);
list.Add(6);
list.Add(5);
list.Add(1);
list.Add(8);
List<int> listOdd = new List<int>();
List<int> listEven = new List<int>();
for (int i = 0; i < list.Count; i++)
{
if (list[i] % 2 != 0)
{
listOdd.Add(list[i]);
}
else
{
listEven.Add(list[i]);
}
}
listOdd.AddRange(listEven);
StringBuilder sb = new StringBuilder();
for(int a=0;a<listOdd.Count;a++)
{
sb.Append(listOdd[a]+"");
}
Console.WriteLine(sb);
Console.ReadKey();
2. 将 int数组中的奇数放到一个新的int数组中返回。
int [] numbers={2,7,9,4,3,8,1,6};
List<int> listOdd=new List<int>();
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 2 != 0)
{
listOdd.Add(numbers[i]);
}
int[] numOdd = listOdd.ToArray();
for (int a = 0; a< numOdd.Length; a++)
{
Console.WriteLine(numOdd[a]);
}
Console.ReadKey();
3 从整数的list中找最大值,不使用max 方法。
List<int> list=new List<int>();
list.Add(10);
list.Add(34);
list.Add(25);
list.Add(87);
list.Add(38);
//Random random=new Random()
//Random.next()
//List.Add();
int temp=0;
for (int i = 0; i < list.Count; i++)
{
if (list[i] > temp)
{
temp = list[i];
}
}
Console.WriteLine("max is:" + temp);
Console.ReadKey();
4 计算字符串中每种字符出现的次数。(面试题[用字典做])“welcome to china,this is a beautiful county ,I think you will like it.here is the Great Wall.
string str = "Welcome to china! this is a beautiful county, i think you will like it.here is The great wall";
str = str.ToLower();//转换成小写、
Dictionary<char, int> dict2 = new Dictionary<char, int>();
//统计出现的字符次数
for (int i = 0; i < str.Length; i++)
{
if (char.IsLetter(str[i]))
{
if (dict2.ContainsKey(str[i]))
{
dict2[str[i]]++;
}
else
{
dict2.Add(str[i], 1);
}
}
foreach (KeyValuePair<char, int> kv in dict2)
{
Console.WriteLine("字符:{0},出现了{1}次", kv.Key, kv.Value);
}
Console.ReadKey();
}
}
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
5 简体繁体字换
private Dictionary<char, char> dict = new Dictionary<char, char>();
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void btnConvert_Click(object sender, EventArgs e)
{
//获取用户输入的简体中文
string input = txtCHS.Text.Trim();
StringBuilder sb=new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
//判断字典中是否包含这个键
if (dict.ContainsKey(input[i]))
{
sb.Append(dict[input[i]]);
}
else
{
//如果dict中不包含,原样输出
sb.Append(input[i]);
}
}
txtCHT.Text = sb.ToString();
}
private void Form1_Layout(object sender, LayoutEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string[] lines=File.ReadAllLines("ST.txt");
for (int i = 0; i < lines.Length; i++)
{
dict.Add(lines[i][0], lines[i][2]);
//lines[0] ---"啊=啊"
}
}
6 把123 转换成壹贰叁
//把 123 变为壹贰叁
Console.WriteLine("请输入1-10的数字");
string number = Console.ReadLine();
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
Dictionary<char, char> dict = new Dictionary<char, char>();
string[] parts = str.Split(' ');
//pars[0]----"1壹"
for (int i = 0; i < parts.Length; i++)
{
dict.Add(parts[i][0], parts[i][1]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < number.Length; i++)
{
sb.Append(dict[number[i]]);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
今天的课还不错,听懂了,作业也自己敲完了。手好累。。。