| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 492 人关注过本帖
标题:[求助修改一下我写的这个程序
只看楼主 加入收藏
er72681085
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2005-10-30
收藏
 问题点数:0 回复次数:2 
[求助修改一下我写的这个程序

/*5.1 随机从控制台输入一串字符,统计各字符出现的次数。*/
using System;
class countstring
{
static void Main()
{
int n=0;
int p=1;
Console.Write("请输入任意字符串:");
string s =Console.ReadLine();
n =s.Length;//获取索引函数的边界
int l = n;
char [] array2=new char[n];//定义char型数组并初始化
char[] array1 = new char[n];
int [] count = new int[n];//
for (int i = 0; i < n; i++)
{
array2[i] = s[i];//把字符串赋值给字符数组
Console.Write("第{0}个为:{1}\t",i+1,array2[i] );//输出字符数组的每一个值
}
Console.WriteLine();
for (int j =0; j < n; j++)
{
p = 1;
for (int k = j + 1; k < n; k++)
{

if (array2[j] == array2[k])
{
l--;
array1[j] = array2[k];

p++;
count[j] = p;
if (count[j] < count[k])
count[j] = count[k];

}
else
continue;
}

Console.WriteLine("你输入的字符串里{0}出现的次数是:{1}",array2[j],count[j]+1);

}
Console.Read();
}
}
帮我看看我写的这个程序,就是红色部分不太合乎题意,看看能怎么改!!

搜索更多相关主题的帖子: 控制台 字符串 统计 
2006-04-18 09:47
飞侠
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-4-18
收藏
得分:0 

using System;
class countstring
{
public static bool tt(char p,ref char[] ar)//判断是否重复

for(int i=0;i<ar.Length;i++)
{
if(p==ar[i])
return false;
}

return true;
}

static void Main()
{
int n=0;
int p=1;
Console.Write("请输入任意字符串:");
string s =Console.ReadLine();
n =s.Length;//获取索引函数的边界
int l = n;
char [] array2=new char[n];//定义char型数组并初始化
char[] array1 = new char[n];
int [] count = new int[n];//
for (int i = 0; i < n; i++)
{
array2[i] = s[i];//把字符串赋值给字符数组
Console.Write("第{0}个为:{1}\t",i+1,array2[i] );//输出字符数组的每一个值
}
Console.WriteLine();
int m=-1;
r (int j =0; j < n; j++)
{
if(tt(array2[j],ref array1))
{
m++;

array1[m]=array2[j];
count[m] = 1;
for (int k = j + 1; k < n; k++)
{

if (array2[j] == array2[k])
{

count[m]++;


}

}
Console.WriteLine("你输入的字符串里{0}出现的次数是:{1}",array1[m],count[m]);

}

}
Console.Read();
}

}

2006-04-18 16:13
鸿鹄之志
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-14
收藏
得分:0 

using System;
using System.Collections.Generic;
using System.Text;

namespace Console1
{
class Program
{
static void Main()
{
int n = 0;
int p = 1;
Console.Write("请输入任意字符串:");
string s = Console.ReadLine();
n = s.Length;//获取索引函数的边界
int l = n;
char[] array2 = new char[n];//定义char型数组并初始化
char[] array1 = new char[n];
char[] count = new char[n];//
for (int i = 0; i < n; i++)
{
array2[i] = s[i];//把字符串赋值给字符数组
Console.Write("第{0}个为:{1}\t", i + 1, array2[i]);//输出字符数组的每一个值
}
Console.WriteLine();
int con = 0;

for (int j = 0; j < n; j++)
{
p = 1;
int index = 0;
bool qq = false;
for (int z = 0; z < con; z++)
{
if(array2[j] == count[z])
{
qq = true;
break;
}
}

if (qq == false)
{
for (int k = n - 1; k > j; k--)
{
if (array2[j] == array2[k])
{
array1[index] = array2[k];
index++;
p++;
}
}
count[con] = array2[j];
con++;
Console.WriteLine("你输入的字符串里{0}出现的次数是:{1}", array2[j], p);
}
}
Console.Read();

}
}
}

2006-04-18 20:41
快速回复:[求助修改一下我写的这个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014479 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved