字数没有重复 [英] Word Count no duplicates

查看:127
本文介绍了字数没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的字计数程序使用java。我需要重新编程这个东西,东西;什么?东西!和一个计数为一个字。这意味着它不应该计数相同的字两次,不管大小写和标点符号。

  import java.util.Scanner; 
public class WordCount1
{
public static void main(String [] args)
{
final int Lines = 6;
Scanner in = new Scanner(System.in);
String paragraph =;
System.out.println(请输入+行+文本行。
for(int i = 0; i< Lines; i + = 1)
{
paragraph = paragraph ++ in.nextLine
}
System.out.println(paragraph);
String word =;
int WordCount = 0;
for(int i = 0; i {
if(paragraph.charAt(i)!= charAt(i)!=','|| paragraph.charAt(i)!=';'|| paragraph.charAt(i)!=':')
{
word = word + paragraph .charAt(i);
if(paragraph.charAt(i + 1)==''|| paragraph.charAt(i)==','|| paragraph.charAt(i)==';'| paragraph.charAt i)==':')
{
WordCount + = 1;
word =;
}
}
}
System.out.println(There are+ WordCount +words);
}
}


解决方案

这是家庭作业,这里有一些提示和建议。




  • 有一个聪明的小方法 String .split 使用指定为正则表达式的分隔符将字符串拆分为多个部分。如果你使用它正确的方式,这将给你一个行解决方案的字数问题。 (如果你被告知不使用split,你可以忽略它 - 虽然这是一个经验丰富的Java开发人员首先考虑的简单解决方案。)


  • 正确格式化/缩进代码,然后将其显示给其他人。


  • 使用标准的Java命名约定。如果您的教师不为此扣除标记,他/她不能正常工作。 Lines 的大写不正确。对于变量,它可以是 LINES 或者,但是以大写字母开头的混合大小写名称


  • 在操作符(包括赋值操作符)使用空白字符时要保持一致。


  • 硬线连接用户必须提供的输入行数是一个坏主意(完全不必要)。



Here is my word count program using java. I need to reprogram this so that something, something; something? something! and something count as one word. That means it should not count the same word twice irregardless of case and punctuation.

import java.util.Scanner;
public class WordCount1
{
    public static void main(String[]args)
    {
        final int Lines=6;
        Scanner in=new Scanner (System.in);
        String paragraph = "";
        System.out.println( "Please input "+ Lines + " lines of text.");
        for (int i=0; i < Lines; i+=1)
        {
            paragraph=paragraph+" "+in.nextLine();
        }
        System.out.println(paragraph);
        String word="";
        int WordCount=0;
        for (int i=0; i<paragraph.length()-1; i+=1)
        {
            if (paragraph.charAt(i) != ' ' || paragraph.charAt(i) !=',' || paragraph.charAt(i)    !=';' || paragraph.charAt(i) !=':' )
            {
                word= word + paragraph.charAt(i);
                if(paragraph.charAt(i+1)==' ' || paragraph.charAt(i) ==','|| paragraph.charAt(i) ==';' || paragraph.charAt(i) ==':')
                {
                    WordCount +=1;
                    word="";
                }
            }
        }
        System.out.println("There are "+WordCount +" words ");
    }
}

解决方案

Since this is homework, here are some hints and advice.

  • There is a clever little method called String.split that splits a string into parts, using a separator specified as a regular expression. If you use it the right way, this will give you a one line solution to the "word count" problem. (If you've been told not to use split, you can ignore that ... though it is the simple solution that a seasoned Java developer would consider first.)

  • Format / indent your code properly ... before you show it to other people. If your instructor doesn't deduct marks for this, he / she isn't doing his job properly.

  • Use standard Java naming conventions. The capitalization of Lines is incorrect. It could be LINES for a manifest constant or lines for variable, but a mixed case name starting with a capital letter should always be a class name.

  • Be consistent in your use of white space characters around operators (including the assignment operator).

  • It is a bad idea (and completely unnecessary) to hard wire the number of lines of input that the user must supply. And you are not dealing with the case where he / supplies less than 6 lines.

这篇关于字数没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆