이곳은 새로운 곳입니다. 나는 도움을 주심에 감사 할 것이고, 문장에서 문장을 찾는 프로그램을 작성하려고 노력하고 있습니다. 이것은 제가 지금까지 가지고있는 것입니다. 그러나, 그것을 실행할 때마다 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++;
}
}
}
}
}
}
}
}
디버거를 사용하여 코드를 이해하기 쉽고 간단한 방법으로 나눕니다. 여기에 질문을 게시하기 전에 할 일이 많습니다. – Idos
힌트 : 코드의 형식을 올바르게 살펴보십시오. 이것은해야 할 것보다 10 배 더 읽기가 어렵습니다. 그리고 : 귀하의 변수에 더 나은 이름을 사용하십시오. s1, s2, s3 ... 그 내용의 내용에 대해 ** 아무 것도 ** 말하지 않습니다. 마지막으로 String.split ("")을 사용하여 문장을 부분으로 나눌 수 있습니다. 당신은 수동으로 공백 문자를 스캔하지 않아도됩니다! – GhostCat
@GhostCat 대단히 감사합니다! 변수 이름을 더 잘 지정하고 업로드하려고했습니다. 그러나 우리는 아직 학교에서 String.split 기능을 수행하지 않았으며 선생님이 승인할지 여부는 확실하지 않지만 다시 시도 할 것입니다. 감사! –