2016-12-04 10 views
-1

ここは新しいですね。私はいくつかの助けに感謝するだろう、私は文章で回文を見つけるためのプログラムを書こうとしている、これは私がこれまで持っているものです。しかし、私が実行するたびに、回文カウントはゼロになり、forとifのSystem.out.printlnは実行されません。何が間違っているのか分かりません。 whileループではなく、forループを使用する必要があります。文章中の回文の数を確認する方法は?

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); 
    } 
} 

はEDIT:私は、プログラムを再実行しているが、その実行得ていない私は、変数の名前を変更しようとしました。私は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、あなたが話しているバグがどこで発生しているのか、あなたが出力を記述することができるかの例を教えていただけますか? –

+0

ああああ、私はどこが間違っていたか理解した!^_ ^;私はsc.nextLine()の代わりにsc.next()を取っていたので、それは完全な文章を読んでいなかった.. Spasibo! –

0

コードでSystaxエラーを回避してください:Javaでは

を、主な機能は以下の

public static void main(String args[]) 
{ 

とループのために持っている必要がありますuはuはのような初期化ループ前の状態に満足決して.becauseを述べました

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

ループ条件が満たされると、内部ブロックのみ実行される。

ループではindexOf()を使用しないでください。 理由は、文字列インデックスが範囲外のときにjava throw StringIndexOutOfBoundsExceptionです。 http://www.vinaysingh.info/palindromic-words/

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

関連する問題