2016-12-04 48 views
-1

我是新来的。我会感谢一些帮助,我正在努力编写一个程序来找到一个句子中的回文,这是我迄今为止所做的。然而,每当我执行它时,回文计数就会变为零,并且for和ifs中的System.out.println不会发生,我不明白什么是错误的。我们必须使用for循环,而不是while。如何检查句子中的回文数?

import java.util.Scanner; 

class palcheck { 
    public void main() { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter the sentence please...");// input sentece 
     String s = sc.next();// the sentence 

     String wordback = ""; //reverse of a word of the sentence 
     int count = 0; // palindrome count 
     String word = "";// for a word of the sentence 
     int a; // counter 
     String s2 = null; //sentence but with removed full stop 

     if (s.charAt(s.length() - 1) == '.')// checking if ends with fullstop 

     { 
      s2 = s.substring(0, (s.length() - 1)); 
     }// sentence without fullstop 
     System.out.println(s2); 
     String s3 = " " + s2 + " "; 
     System.out.println(s3);// added a space; 
     for (int c = 0; c < s3.length(); c++) { 
      char isspace = s3.charAt(c); 
      if (isspace == ' ')// checking 
      { 
       char x = ' ';// initilaizing 
       for (a = s3.indexOf(isspace); x != ' '; a++) { 
        x = s3.charAt(a); //collecting letters for word 
        word = word + x; // forming word 
        System.out.println("word is " + word);// the formed word 
       } 
       int l2 = word.length(); 
       for (int i = l2 - 1; i >= 0; i--) { 
        wordback = wordback + word.charAt(i);// forming reverse word 
        System.out.println("reverse word is " + wordback); 
        if (word.equalsIgnoreCase(wordback)) { 
         count = count + 1; 
        }// increasing palindrome count 
        word = null;// resetting value 
        wordback = null;//resetting value 
       } 
      } 
     } 
     System.out.println("the no of palindromes is " + count); 
    } 
} 

编辑:我试图改变变量的名称:

import java.util.Scanner; 

class palcheck { 
    public void main() { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter the sentence please...");// input sentece 
     String sentence = sc.next();// the sentence 

     String wordreverse = ""; //reverse of a word of the sentence 
     int count = 0; // palindrome count 
     String word = "";// for a word of the sentence 
     int a; // counter 
     String sentencewithoutfullstop = null; //sentence but with removed full stop 

     if (sentence.charAt(sentence.length() - 1) == '.')// checking if ends with fullstop 

     { 
      sentencewithoutfullstop = sentence.substring(0, (sentence.length() - 1)); 
     }// sentence without fullstop 
     System.out.println(sentencewithoutfullstop); 
     String sentencewithspace = " " + sentencewithoutfullstop + " "; 
     System.out.println(sentencewithspace);// added a space; 
     for (int c = 0; c < sentencewithspace.length(); c++) { 
      char isspace = sentencewithspace.charAt(c); 
      if (isspace == ' ')// checking 
      { 
       char letter = ' ';// initilaizing 
       for (a = sentencewithspace.indexOf(isspace); letter != ' '; a++) { 
        letter = sentencewithspace.charAt(a); //collecting letters for word 
        word = word + letter; // forming word 
        System.out.println("word is " + word);// the formed word 
       } 
       int length2 = word.length(); 
       for (int i = length2 - 1; i >= 0; i--) { 
        wordreverse = wordreverse + word.charAt(i);// forming reverse word 
        System.out.println("reverse word is " + wordreverse); 
        if (word.equalsIgnoreCase(wordreverse)) { 
         count = count + 1; 
        }// increasing palindrome count 
        word = null;// resetting value 
        wordreverse = null;//resetting value 
       } 
      } 
     } 
     System.out.println("the no of palindromes is " + count); 
    } 
} 

编辑:我重做的计划,但它没有得到执行。我正在使用bluej,并且每当我被选中void(main),java虚拟机都会继续运行,我必须终止它。

程序中有问题吗?

import java.util.Scanner; 

class cal { 
    public static void main(String args[]) { 
     Scanner sc = new Scanner(System.in); 
     String input = sc.next(); 
     String word = null; 
     String reverse = null; 
     int count = 0; 
     if (input.endsWith(".")) { 
      String Sent2 = input.substring(0, (input.length() - 1)); 
      String sent3 = " " + Sent2 + " "; 
      int l = sent3.length(); 
      for (int i = 0; i < l; i++) { 
       if (sent3.charAt(i) == ' ') { 
        while (sent3.charAt(i + 1) != ' ') { 
         char h = sent3.charAt(i + 1); 
         word = word + h; 
         int l2 = word.length(); 
         for (int j = l2 - 1; j >= 0; j--) { 
          char hg = word.charAt(j); 
          reverse = reverse + hg; 
          if (word.equalsIgnoreCase(reverse)) { 
           System.out.println("palindrome :" + word); 
           count++; 
          } 
         } 
        } 
       } 
      } 
     } else { 
      String sent3 = " " + input + " "; 
      int l = sent3.length(); 
      for (int i = 0; i < l; i++) { 
       if (sent3.charAt(i) == ' ') { 
        while (sent3.charAt(i + 1) != ' ') { 
         char h = sent3.charAt(i + 1); 
         word = word + h; 
         int l2 = word.length(); 
         for (int j = l2 - 1; j >= 0; j--) { 
          char hg = word.charAt(j); 
          reverse = reverse + hg; 
          if (word.equalsIgnoreCase(reverse)) { 
           System.out.println("palindrome :" + word); 
           count++; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+1

使用调试器,将您的代码划分为可理解和简短的方法...在发布您的问题之前需要做的事情还有很多。 – Idos

+2

并提示:仔细查看格式正确的代码。这是比阅读更难以阅读的东西的10倍。并且:为你的变量使用更好的名字。 s1,s2,s3 ...对这些东西的内容说**没有任何**。最后:您可以使用String.split(“”)将句子分成几部分;你不必手动扫描空间字符! – GhostCat

+0

@GhostCat非常感谢你!我试图更好地命名变量并将其上传。但是,我们还没有在学校完成String.split功能,我不确定我们的老师是否会赞成,但是我会再试一次。谢谢! –

回答

0

此代码假定你有一个inputString,发现里面的回文。

String word = ""; 
String inverse = ""; 
for (int index = 0; index < input.length(); index++) { 
    if (input.charAt(index) == " ") { 
     if ((word.length() > 0) && (word.equals(inverse))) { 
      System.out.println("Palindrome: " + word); 
     } 
     word = ""; 
     inverse = ""; 
    } else { 
     char current = input.charAt(index); 
     word += current; 
     inverse = current + inverse; 
    } 
} 
if ((word.length() > 0) && (word.equals(inverse))) { 
    System.out.println("Palindrome: " + word); 
} 
+0

非常感谢!然而,无论何时我试图用这种方式执行它,只有第一个单词被识别为回文,如果它是回文。有没有办法来解决这个问题? –

+0

@viktornikiforov,你可以给我一个示例输入来说明你正在谈论的bug是否发生,并且你能描述你得到的输出吗? –

+0

啊,它确定,我明白我要去哪里错了!^_ ^;我正在采取sc.next()而不是sc.nextLine(),因此它没有读完整句。Spasibo! –

0

请避免Systax错误代码:

在Java中,主要功能必须具备以下

public static void main(String args[]) 
{ 

和支路u循环ü初始化像前面提到的条件永不满足。因为,

char letter = ' ' ;// initilaizing 
for(a = sentencewithspace.indexOf(isspace); letter !=' ' ;a++) 

一旦环路条件满足,那么只有块内部执行。

请不要在循环中使用indexOf()。 原因是java throw StringIndexOutOfBoundsException当String index超出范围。

参考链接:http://www.vinaysingh.info/palindromic-words/

how to count the number of words of the same(PALINDROME) in java