回 帖 发 新 帖 刷新版面

主题:[讨论]修改一下我写的程序

/*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();
  [color=800000]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);[/color]  }
      Console.Read();
 }
}
帮我看看我写的这个程序,就是红色部分不太合乎题意,看看能怎么改!!

回复列表 (共3个回复)

沙发

Console.WriteLine("请输入您要检测的字符串:");
            string testString=Console.ReadLine();
            int[] resultList=new int[testString.Length];
            char[] charResultList=new char[testString.Length];
            for(int j=0;j<testString.Length;j++)
            {
                int num=0;
                char c=testString[j];
                for(int i=0;i<testString.Length;i++)
                {
                    char cc=testString[i];
                    if(cc==c)
                    {
                        testString=testString.Remove(i,1);
                        i--;
                        num+=1;
                    }

                }
                charResultList[j]=c;
                resultList[j]=num;
            }
            for(int i=0;i<resultList.Length;i++)
            {
                if((int)charResultList[i]==0)
                    break;
                else
                    Console.WriteLine("字符{0}出现个数分别为:{1}",charResultList[i],resultList[i]);
            }
            Console.Read();

        }

板凳

/*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();
        int m = 0;
        array1[0] = array2[0];//第一次赋值
        for (int j = 0; j < n; j++)
        {
            int flog = 0;
            if (j != 0) //第一次已赋值
            {
                for (int i = 0; i <= m; i++)
                    if (array1[i] == array2[j])
                        flog = 1;                    
                if (flog == 0)
                {
                    m++;
                    array1[m] = array2[j];
                }
                if (flog == 1) //说明array2[j]已统计过了
                    continue;
            }
            p = 0;
            for (int k = j; k < n; k++)
            {
                if (array1[m] == array2[k])
                {
                    l--;
                    p++;                    
                }
                count[m] = p;                                   
            }
        }
        for (int j = 0; j <= m;j++ )
            Console.WriteLine("你输入的字符串里'{0}'出现的次数是:{1}", array1[j], count[j] );
         }
    }

一楼的程序似乎还有点问题,试试我这个.

3 楼


/*5.1 随机从控制台输入一串字符,统计各字符出现的次数。*/
using System;
class countstring
{
    static void Main()
    {        
        Console.WriteLine("请输入您要检测的字符串:");
               string testString=Console.ReadLine();
               int[] resultList=new int[testString.Length];
               char[] charResultList=new char[testString.Length];
               for(int j=0;j<testString.Length;j++)
               {
                   int [color=FF0000]num=1;[/color]
                   char c=testString[j];
                   for(int [color=FF0000]i=j+1;[/color]i<testString.Length;i++)
                   {
                       char cc=testString[i];
                       if(cc==c)
                       {
                           testString=testString.Remove(i,1);
                           i--;
                           num+=1;
                       }
                   }
                   charResultList[j]=c;
                   resultList[j]=num;
               }
               for(int i=0;i<resultList.Length;i++)
               {
                   if((int)charResultList[i]==0)
                       break;
                   else
                       Console.WriteLine("字符{0}出现个数分别为:{1}",charResultList[i],resultList[i]);
               }
    }
}

一楼的我改了一下,应该没问题了,一楼基础好,但有点大意

我来回复

您尚未登录,请登录后再回复。点此登录或注册