【问题标题】:Java Sentence Analysis CheckJava 语句分析检查
【发布时间】:2017-03-09 08:26:47
【问题描述】:

嗯,我的老师给了我一个java作业。他想要一个分析句子的程序。我写了代码,但它不起作用。我做错了什么你能检查一下吗? (嗯,我花了很多时间自学java并编写这些代码,所以这对我来说意义重大!)

对于页面 Ödev1.java ==>

package Ödev1;

import javax.swing.JOptionPane;

public class Ödev1 {

    public static void main(String[] args) {
        String input = JOptionPane.showInputDialog("Enter a sentence");
        System.out.print(input);
      odev1 cumle = new odev1();
        cumle.analiz();
    }
}

对于页面 odev1.java ==>

package odev1;

public class odev1 {
    String input;
    int characters;
    int words;
    int gaps;
    int lowerletters;
    int capitalletters;
    int vowels;
    int sibilancies;

    public odev1(int characters, int words, int gaps, int lowerletters, int capitalletters, int vowels, int sibilancies) {
        this.characters = characters;
        this.words = words;
        this.gaps = gaps;
        this.lowerletters = lowerletters;
        this.capitalletters = capitalletters;
        this.vowels = vowels;
        this.sibilancies = sibilancies;
    }

  public void analysis()
  {
characters=input.length();
       char[] array = input.toCharArray();
int n=0;
for(int a=0;a<characters;a++) {
do{
n=n+1;
}while(array[a]!='0'); }
words=characters-n;
gaps=words-1;
capitalletters=0;
lowerletters=0;
for(int b=0;b<characters;b++) {
     if (Character.isUpperCase(input.charAt(b))) capitalletters++;
     if (Character.isLowerCase(input.charAt(b))) lowerletters++; }
vowels=0;
for(int c=0; c< input.length();c++) {
    switch(input.charAt(c)) {
        case 'a':
        case 'e':
        case 'ı':    
        case 'i':
        case 'o':
        case 'ö':
        case 'u':
        case 'ü':
            vowel++;
            break;            }
}
sibilancies=characters-gaps-vowels;
System.out.println("Count of characters="+characters);
System.out.println("Count of words="+words);
System.out.println("Count of gaps="+gaps);
System.out.println("Count of capital letters="+capitalletters);
System.out.println("Count of lower letters="+lowerletters);
System.out.println("Count of vowels="+vowels);
System.out.println("Count of sibilancies="+sibilancies);
}
}  

【问题讨论】:

  • 您显然知道如何将代码复制/粘贴到问题中,因此请为所有相关代码这样做。不要插入指向代码截图的链接。 --- 提示: 修正代码的缩进,使其可读。并且在粘贴到问题时,额外缩进 4 个空格,以便代码在此处正确格式化。
  • 这个地方太多了,我的代码是可读的,你为什么这么想?
  • 我不知道“有太多地方可以做”是什么意思。 “太少”可能是有道理的,除了有足够的空间来缩进你的代码。缩进代码以显示代码的 结构 使其更具可读性且不易出错。这就是为什么我(和大多数其他人)那样想的原因。至于屏幕截图的链接,它只是不好/懒惰,如果链接服务器关闭时问题变得毫无意义。此外,“它不起作用”是对您的问题的糟糕描述!!!!
  • 扩展/解释@Andreas 所评论的内容。请对代码和代码 sn-ps、结构化文档(如 HTML/XML 或输入/输出)使用代码格式。为此,选择文本并单击消息发布/编辑表单顶部的{} 按钮。
  • @Andreas “不要插入指向代码截图的链接。” 这么好的建议,我认为值得重复,.. 这次在粗体。

标签: java string text


【解决方案1】:

您的代码很混乱,但据我了解,您想分析某个短语。

首先,处理分析的类的构造函数必须接收String 进行分析,因为这是它的工作。 (每个类都有一个工作,它的结果是从类方法中获得的)。

其次,Java 命名有一个约定,类的名称以大写字母开头。查看 Oracle 的此文档:http://www.oracle.com/technetwork/java/codeconventions-135099.html

所以,类应该是这样的:

public class Odev1 {

    private final String phase;

    public Odev1( String phrase ) {
        this.phrase = phrase;
    }

    //Remaining code...
}

至于分析本身,您应该记住几件事:

首先,您不必从String 创建char 的数组,您可以直接使用StringcharAt( int index ) 方法获取char

其次,当你有一个 for 循环时,你可以使用相同的字母,因为变量是局部的,因此它们在 for 循环之外是不可见的

第三,当你构造一个循环时,你必须确保你可以跳出循环。例如,您写道:

int n=0;
for(int a=0;a<characters;a++) {
    do{
        n=n+1;
    }while(array[a]!='0'); 
}
words=characters-n;

就在这里你有一个无限循环。为什么你在做循环同样的char?按照这个: int n = 0; => int a=0; => do => n = n + 1 -> n = 1 => 条件 while => array[a]!='0' -> true => 重复 do-while 循环......永远...

相反,您可以遍历 String 并使用 if-else 条件对其进行分析。像这样的:

int n=0;
boolean isWord = false; //Use it to track the letters
boolean isSpace = false;
for(int i=0;i<characters;i++) {
    char charToAnalyse = input.getCharAt( i );

    if( charToAnalyse == ' ' ) { //Taking the simple case where you don't have breaklines
        isSpace = true;
        isWord = false;
    }
    else {
        isSpace = false;
        if( !isWord ) {
            isWord = true; //once it is counted, we should avoid recount.
            n++; //Its is the same as n = n + 1;
        }
    }
}
words=characters-n;

作为一种更简单的方法,您可以使用split(String regex) 方法,但我给出了上面的示例以帮助您理解。

String[] wordsArray = input.split( " " );
words = wordsArray.lenght;

第四,如果包含一个意味着排除其余部分,则使用else-if 而不是经常使用if。也就是说,在您上面的代码中,您遍历字母以查看它们是否为大写。

此外,您不必为每个变量编写循环。每个字符都有几个属性,所以只需在一个循环中检查它们:

int capitalletters = 0;
int lowerletters = 0;
int vowel = 0;

for(int i=0;i<characters;i++) {
    char charToAnalyse = input.charAt(i);

    if (Character.isUpperCase( charToAnalyse )) { 
        capitalletters++; 
    }
    else if (Character.isLowerCase( charToAnalyse  )){ 
        lowerletters++; 
    }
    //Imagine if there were many if statement and the condition 
    //was already true in the first...

    //count vowels;
    switch( charToAnalyse  ) {
        case 'a':
        case 'e':
        case 'ı':    
        case 'i':
        case 'o':
        case 'ö':
        case 'u':
        case 'ü':
            vowel++;
        break; //You can omit this one if it is at the end;
    }

}

最后,要使用 Odev1 类,只需:

Odev1 cumle = new Odev1( input );
cumle.analyse();

我希望我有所帮助。

祝你有美好的一天。 :)

【讨论】:

  • “但我举了上面的例子来帮助你理解。” 干得好。再加上一个,为您提供全面的答案和解释。
  • 这对我很有帮助!你太棒了太棒了,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
相关资源
最近更新 更多