日期:2014-05-17 浏览次数:20937 次
private void NewMethod() { List<string> list = new List<string>();//List string s = string.Empty; if (textBox1.Text.Trim() != "") { char[] c = textBox1.Text.Trim().ToCharArray(); for (int i = 0; i < c.Length; i++) { int count = 0; for (int j = 0; j < c.Length; j++)//相同的累加 { if (c[i] == c[j]) count++; } s = c[i] + "=" + count; if (list.IndexOf(s) == -1)//如果存在就不用加 { list.Add(s); this.textBox2.Text += s + "\r\n"; } } } }
------解决方案--------------------
不用list,用list是多余的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Countstring
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您要查询的字符串:\n");
string s = Console.ReadLine();
Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
int count=0;
for (int i = 0; i < s.Length;i++ )
{
//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
int frontifsame = 0;
for (int j = i - 1; j >= 0; j--)
{
if (s[j] == s[i])
{
frontifsame++;
break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
}
}
if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
{
count = 1;
for (int j = i + 1; j < s.Length; j++)
{
if (s[j] == s[i])
{
count++;
}
}
Console.WriteLine("{0} 在您输入的字符串中出现的次数为: {1}.\n", s[i], count);
}
}
}
}
}
------解决方案--------------------
键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。
都不很好 用sortlist
static void Main(string[] args) { string data = "asd646uio4asda .sdjhglajsdoajam] 4uio64aaaksd;ka;s"; SortedList<char, int> charCount = new SortedList<char, int>(); foreach (char i in data) { if (!charCount.ContainsKey(i)) { charCount.Add(i, 1); } else { charCount[i]=charCount[i]+1; } } foreach (KeyValuePair<char, int> i in charCount) { Console.WriteLine(" "+i.Key+": "+i.Value); } }
------解决方案--------------------
粗略做了个,lz看看
class Program
{
public static int numWord(string word, string[] wordList)//查询字母在字符数组中出现的次数查询
{
int cout = 0;
for (int i = 0; i < wordList.Length; i++)
{
if (word.Equals(wordList[i], St