2016-12-04 2 views
-1

이곳은 새로운 곳입니다. 나는 도움을 주심에 감사 할 것이고, 문장에서 문장을 찾는 프로그램을 작성하려고 노력하고 있습니다. 이것은 제가 지금까지 가지고있는 것입니다. 그러나, 그것을 실행할 때마다 palindrome 카운트는 0이되고 for와 if의 System.out.println은 발생하지 않고 무엇이 잘못되었는지를 이해할 수 없습니다. 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, 당신이 말하고있는 버그가 어디에서 발생했는지, 그리고 출력을 설명 할 수있는 예제 입력을 해줄 수 있습니까? –

+0

아아 그 ok, 나는 내가 잘못 가고 있었던 것을 이해했다!!^_ ^; sc.nextLine() 대신 sc.next()를 사용 했으므로 전체 문장을 읽지 못했습니다. Spasibo! –

0

코드 Systax 오류를 피하십시오

자바에서

는, 주요 기능은 다음과

public static void main(String args[]) 
{ 

이 있어야하며 루프 유 조건이 유 같은 초기화 루핑 전에 결코 만족 .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

관련 문제