| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2948 人关注过本帖
标题:求求哪位大神帮帮忙,一直求不出the的个数%>_<%
只看楼主 加入收藏
苏苏酱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-4-24
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:11 
求求哪位大神帮帮忙,一直求不出the的个数%>_<%
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
import *;
import java.util.*;
public class End {
    private int line, charCount, countThe;
    private StringBuffer sb;
    private Vector<String> v;
    private Vector<StringBuffer> vsb;

    public End() {
        sb = new StringBuffer();
        v = new Vector<String>();
        vsb = new Vector<StringBuffer>();
    }

    public void input() throws IOException {
        String s = "";
        System.out.println("请输入若干行文本,以end作为结束行:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        s = br.readLine();
        while (!s.equals("end")) {
            v.add(s);
            StringBuffer ssb = new StringBuffer(s);
            vsb.add(ssb);
            s += "\n";
            line++;
            sb.append(s);
            s = br.readLine();
        }
    }

    public void count() {
        for (int i = 0; i < v.size(); i++) {        
            String[]s = v.get(i).split("");
            String ss = "";
            for (int j = 0; j < s.length; j++) {
                if (s[j].contains("the"))
                    countThe++;                
                ss+=s[j];                
            }
            charCount += ss.length();            
        }
    }//问题

    public void translate() {
        for (int i = 0; i < vsb.size(); i++) {
            for (int j = 0; j < vsb.get(i).length(); j++)
                if (j == 0 || vsb.get(i).charAt(j - 1) == ' ')
                    vsb.get(i).setCharAt(j, Character.toUpperCase(vsb.get(i).charAt(j)));
        }
    }

    public void output() {
        System.out.println("该文本由" + line + "行组成,字符总数为:" + charCount + ",有 " +countThe+ "个“the”");
        System.out.println("将整个文本中所有单词首字母为小写的改为大写输出:");
        System.out.println(vsb.toString());
    }

    public static void main(String[] args) throws IOException {
        End e = new End();
        e.input();
        e.count();
        e.translate();
        e.output();
    }
}

搜索更多相关主题的帖子: Vector 
2016-05-11 20:42
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:4 
如果仅仅是找单词中包含的the,可以这样写,如果是要包含独立的单词the,那么需要小改一下。

程序代码:
import *;
import java.util.*;
public class End {
     private int line, charCount, countThe;
     private StringBuffer sb;
     private String str;
     private Vector<String> v;
     private Vector<StringBuffer> vsb;

     public End() {
         sb = new StringBuffer();
         v = new Vector<String>();
         vsb = new Vector<StringBuffer>(); 
         str = new String();
     }

     public void input() throws IOException {
         String s = "";
         System.out.println("请输入若干行文本,以end作为结束行:");
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         s = br.readLine();
         str = "";
         while (!s.equals("end")) {
             v.add(s);
             str = str + s;
             StringBuffer ssb = new StringBuffer(s);
             vsb.add(ssb);
             s += "\n";
             line++;
             sb.append(s);
             s = br.readLine();
         }
     }

     public void count() {
         charCount = str.length();
         str = str.replace("the", "");
         countThe = (charCount - str.length())/"the".length();
         /*
         for (int i = 0; i < v.size(); i++) {        
             String[] s = v.get(i).split("");
             String ss = "";            
                    
             for (int j = 0; j < s.length; j++) {
                 if (s[j].contains("the"))
                     countThe++;                
                 ss+=s[j];                
             }
             charCount += ss.length();            
         }*/
     }//问题

    public void translate() {
         for (int i = 0; i < vsb.size(); i++) {
             for (int j = 0; j < vsb.get(i).length(); j++)
                 if (j == 0 || vsb.get(i).charAt(j - 1) == ' ')
                     vsb.get(i).setCharAt(j, Character.toUpperCase(vsb.get(i).charAt(j)));
         }
     }

     public void output() {
         System.out.println("该文本由" + line + "行组成,字符总数为:" + charCount + ",有 " +countThe+ "个“the”");
         System.out.println("将整个文本中所有单词首字母为小写的改为大写输出:");
         System.out.println(vsb.toString());
     }

     public static void main(String[] args) throws IOException {
         End e = new End();
         e.input();
         e.count();
         e.translate();
         e.output();
     }

 }



请输入若干行文本,以end作为结束行:
the
other
another
end
该文本由3行组成,字符总数为:15,有 3个“the”
将整个文本中所有单词首字母为小写的改为大写输出:
[The, Other, Another]
2016-05-12 10:56
mary_xiaoman
Rank: 2
来 自:湖南岳陽
等 级:论坛游民
威 望:3
帖 子:13
专家分:55
注 册:2010-12-21
收藏
得分:4 
   有问题的是这段代码:

            for (int j = 0; j < s.length; j++) {
                if (s[j].contains("the"))
                    countThe++;
                ss += s[j];
            }              

           你用s[j]去匹配the,  s[j]每次循环进来的都是字符t、h、e  所以一直没进这个if语句,countThe才会为0

解决方法:

1.匹配整个字符都等于the
        for (int i = 0; i < v.size(); i++) {
            String ss = v.get(i);
            if("the".equals(ss)){
                countThe++;  
            }
            charCount += ss.length();
        }

请输入若干行文本,以end作为结束行:
the
athe
end
该文本由2行组成,字符总数为:7,有 1个“the”
将整个文本中所有单词首字母为小写的改为大写输出:
[The, Athe]


2.只要字符中包含the

        for (int i = 0; i < v.size(); i++) {
            String ss = v.get(i);
            if(ss.contains("the")){
                countThe++;  
            }
            charCount += ss.length();
        }

请输入若干行文本,以end作为结束行:
the
sthe
athe
end
该文本由3行组成,字符总数为:11,有 3个“the”
将整个文本中所有单词首字母为小写的改为大写输出:
[The, Sthe, Athe]

[此贴子已经被作者于2016-5-12 11:21编辑过]

2016-05-12 11:01
苏苏酱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-4-24
收藏
得分:0 
回复 3楼 mary_xiaoman
第一种方法不可以用,能不能将你运行的代码发给我O(∩_∩)O!
2016-05-12 20:31
苏苏酱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-4-24
收藏
得分:0 
回复 2楼 grmmylbs
程序存在bug,不同行可以,同一行就不可以了
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2016-05-13 09:06
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
为啥我可以呢

请输入若干行文本,以end作为结束行:
the other another
end
该文本由1行组成,字符总数为:17,有 3个“the”
将整个文本中所有单词首字母为小写的改为大写输出:
[The Other Another]
2016-05-13 09:24
mary_xiaoman
Rank: 2
来 自:湖南岳陽
等 级:论坛游民
威 望:3
帖 子:13
专家分:55
注 册:2010-12-21
收藏
得分:0 
回复 4楼 苏苏酱
    应该知道你想要什么效果了,其它保持不变只需要把你原代码v.get(i).split(" ")括号内加上空格即可,如下:


   
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2016-5-13 10:47编辑过]

2016-05-13 10:43
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:3 
程序代码:
package test1;

import import java.util.Scanner;

public class End {
    static StringBuilder sb=new StringBuilder();
    static int rows,words,word_count=0;
    public static void output() {
        System.out.println("该文本由" + rows + 
                "行组成,字符总数为:" + words +
                ",有 " +word_count+ "个“the”");
        System.out.println("将整个文本中所有单词首字母"
                + "为小写的改为大写输出:");
        System.out.println(sb.toString());
    }
    public static void input(){
        Scanner scan=new Scanner(System.in);
        String str;
        while(!(str=scan.nextLine()).equals("end")){
            rows++;
            words+=str.length();
            for(int i=0;i<str.length();i++){
                char ch=str.charAt(i);
                if(i==0||str.charAt(i-1)==' ')
                    ch=Character.toUpperCase(ch);
                sb.append(ch);
                if(i>2&&str.substring(i-3, i).equals("the"))
                    word_count++;
            }
            sb.append('\n');
        }
        scan.close();
    }
    public static void main(String[] args) throws IOException {
       input();
       output();
    }
}

剑栈风樯各苦辛,别时冰雪到时春
2016-05-13 14:55
苏苏酱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-4-24
收藏
得分:0 
回复 6楼 grmmylbs
这真是一个神奇的问题,那如果我要实现单个“the”单词的话,只需要把 if (s[j].contains("the"))换成 if (s[j].equals("the"))就可以了么?
2016-05-13 17:54
苏苏酱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-4-24
收藏
得分:0 
回复 7楼 mary_xiaoman
恩,好的这样可以了,可以实现包含“the”单词,但是对单个“the”单词,我把contains("the"))换成equals("the")为什么不可以呢?
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2016-5-13 18:08编辑过]

2016-05-13 18:05
快速回复:求求哪位大神帮帮忙,一直求不出the的个数%>_<%
数据加载中...
 
   



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

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