| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1116 人关注过本帖
标题:方法定义出错?????
只看楼主 加入收藏
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:13 
方法定义出错?????
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int count;
            int count1;
            int count2;
            string Repeat;
            int count3;
            string  count4;
            textualEdit text = new textualEdit();       //初始化

            text.str = Console.ReadLine();  //输入一段字符

            //输入一页文章
            count = text.count(text .str );            
            Console.Write("请输入一串字符");            
            Console.WriteLine(count );

            //文章中出现空格个数
            count1 = text.blankSpace(text .str );
            Console.Write("出现空格个数");
            Console.WriteLine(count1);

            //某个字符串在文章中出现的次数
            Repeat = Console.ReadLine();
            count2 = text.repeat(text.str, Repeat);
            Console.Write("某个字符串在文章中出现的次数");
            Console.WriteLine(count2);

            //数字在文章中出现的次数
            count3 = text.amount(text .str );
            Console.Write("数字在文章中出现的次数");
            Console.WriteLine(count3);

            //删除的某一子串后的字符串
            text .st = Console.ReadLine();
            count4 = text.Dele(text .str ,text .st );
            Console.Write("删除的某一子串后的字符串");
            Console.WriteLine(count4);



        }
    }
    public class textualEdit
    {
       public  string str = " ";   //存储一页文章
       public  string s;           // 统计在文章中要查的重复的字符串
       public  string st;          //  要删除的字符串
        public int count(string str)       //统计文章总字数
        {
            this.str = str;
            int Count=0;
            Count = str.Length;
            return Count;

        }
        public int blankSpace(string str)       //统计在文章中出现空格个数
        {
            int count1 = 0;
            this.str = str;
            char[] cara = str.ToCharArray();
            for (int i = 0; i < cara.Length; i++)
            {
                string number=cara[i].ToString ();
                if (number == " ")
                {
                    count1++;
                }
            }
            return count1;
        }
       public int repeat(string str,string s)       //统计某个字符串在文章中出现的次数
        {
            int count3 =0;
            this.str = str;
            this.s = s;
           
            char[] cara = str.ToCharArray();
           
            for (int i = 0; i < cara.Length; i++)
            {
                    if (s .Equals (cara[i]))
                    {
                        count3++;
                    }
               
            }
            return count3;
            
        }
        public int amount(string str)       //统计在文章中出现数字个数
        {
            int count2 = 0;
            this.str = str;
            char[] cara = str.ToCharArray();
            for (int i = 0; i < cara.Length; i++)
            {
                if (cara[i] >= 0 && cara[i] <= 9)
                {
                    count2++;
                }
            }
            return count2;
        }
        public string Dele(string str, string st)        //文章中要删除的某一子串
        {
            this.str = str;
            this.st = st;
            string Del =str.Replace(st,"");
            return Del;


        }

    }
}
数字在文章中出现的次数,某个字符串在文章中出现的次数都无法实现,应该是方法定义的逻辑出了问题,但是不会改,求路过的大神帮忙改改。。。

搜索更多相关主题的帖子: count 文章 
2013-06-19 23:08
csharpluntan
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:7
帖 子:197
专家分:1122
注 册:2013-4-24
收藏
得分:10 
比较时候用ASCII码值才可以


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

namespace tongji
{
    class Program
    {
        static void Main(string[] args)
        {
            int count;
            int count1;
            int count2;
            string Repeat;
            int count3;
            string  newstring;
            textualEdit text = new textualEdit();
            //初始化
            Console.WriteLine("请输入一串字符:");
               
            text.str = Console.ReadLine();  //输入一段字符

            //输入一页文章
            count = text.count(text .str );            
            Console.Write("字符串长度:");            
            Console.WriteLine(count );

            //文章中出现空格个数
            count1 = text.blankSpace(text .str );
            Console.Write("出现空格个数:");
            Console.WriteLine(count1);

            //某个字符串在文章中出现的次数
            Console.Write("请输入要查询重复次数的字符:");
            Repeat = Console.ReadLine();
            count2 = text.repeat(text.str, Repeat);
            Console.Write("{0}在文章中出现的次数:",Repeat);
            Console.WriteLine(count2);

            //数字在文章中出现的次数
            Console.Write("数字在文章中出现的次数");
            count3 = text.amount(text .str );
         
            Console.WriteLine(count3);

            //删除的某一子串后的字符串
            Console.Write("请输入将要删除子字符串:");
            text .st = Console.ReadLine();
          newstring = text.Dele(text .str ,text .st );
            Console.Write("删去后字符串变为:");
            Console.WriteLine(newstring);
            Console.ReadKey();


        }
    }
    public class textualEdit
    {
       public  string str = " ";   //存储一页文章
       public  string s;           // 统计在文章中要查的重复的字符串
       public  string st;          //  要删除的字符串
        public int count(string str)       //统计文章总字数
        {
            this.str = str;
            int Count=0;
            Count = str.Length;
            return Count;

        }
        public int blankSpace(string str)       //统计在文章中出现空格个数
        {
            int count1 = 0;
            this.str = str;
            char[] cara = str.ToCharArray();
            for (int i = 0; i < cara.Length; i++)
            {
                string number=cara[i].ToString ();
                if (number == " ")
                {
                    count1++;
                }
            }
            return count1;
        }
       public int repeat(string str,string s)       //统计某个字符串在文章中出现的次数
        {
            int count3 =0;
            
            char[] cara = str.ToCharArray();
           
            for (int i = 0; i < cara.Length; i++)
            {
                    if (s .Equals (cara[i]))
                    {
                        count3++;
                    }
               
            }
            return count3;
            
        }
        public int amount(string str)       //统计在文章中出现数字个数
        {
            int count2 = 0;
            this.str = str;
            char[] cara = str.ToCharArray();
            for (int i = 0; i < cara.Length; i++)
            {
                if (cara[i] >= 48&& cara[i] <= 57)
                {
                    count2++;
                }
            }
            return count2;
        }
        public string Dele(string str, string st)        //文章中要删除的某一子串
        {
            this.str = str;
            this.st = st;
            string Del =str.Replace(st,"");
            return Del;


        }

    }
}

投之以桃,报之以李
2013-06-20 10:31
lhb62232397
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:117
专家分:546
注 册:2011-11-6
收藏
得分:5 
cara[i]是个char,楼上的修改的没有错。
但是统计某个字符串在文章中出现的次数
这个函数好像逻辑不对吧
2013-06-20 11:02
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:5 
如果就統一出現的次數不用那麼麻煩的!

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-20 11:37
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:0 
回复 3楼 lhb62232397
是啊,public int repeat(string str,string s)       //统计某个字符串在文章中出现的次数
        {
            int count3 =0;
            this.str = str;
            this.s = s;
           
            char[] cara = str.ToCharArray();
           
            for (int i = 0; i < cara.Length; i++)
            {
                    if (s .Equals (cara[i]))
                    {
                        count3++;
                    }
               
            }
            return count3;
            
        }
但是不会改,能帮帮忙吗??

你不勇敢,谁替你坚强。。。
2013-06-20 11:56
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:0 
回复 4楼 QJlin
那该怎么写啊??给点意见喂。。。

你不勇敢,谁替你坚强。。。
2013-06-20 11:57
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:0 
还有,现在发现一个问题就是删除的时候只能删除后面的,前面的或者中间的都删除不了诶。。。

你不勇敢,谁替你坚强。。。
2013-06-20 12:12
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:15 
用集合统计最简单,又方便,它delete,contain两个方法应该可以满足你要求

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-20 12:19
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:0 
回复 8楼 QJlin
哦哦。。。那我试一下,谢谢了。。。

你不勇敢,谁替你坚强。。。
2013-06-20 12:22
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:15 
统计字符串出现的次数
待统计字符串,如abcdefgabcfdlskgjabc
统计abc出现的次数

1 求abcdefgabcfdlskgjabc总长度,这里长度是20
2 删除字符串abc
3 删除以后字符串为defgfdlskgj,求出删除之后的长度,现在长度是11
4 (原长度 - 删除后长度)/删除字符串的长度,即(20-11)/3 = 3,那么abc出现次数为3

[ 本帖最后由 yhlvht 于 2013-6-20 14:44 编辑 ]
2013-06-20 14:29
快速回复:方法定义出错?????
数据加载中...
 
   



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

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