逆向拼写单词并识别回文数的书写方法
Writing method that spells word backwords and identifies number of palindromes
我是 java 的新手,我写了这个方法来输入一个字符串单词并输出反向拼写的单词。目的是创建 一种方法,而不是使用已经存在的方法,例如简单的reverse。请帮我指明如何反转单词的方向。如果有回文,我也在尝试 determine/count。请帮忙!我已经阅读了其他问题,但找不到足够具体的内容来说明我的情况。我知道我的代码没有 运行,但我不确定如何修复它以获得正确的输出。
一个例子是 "backwards" 转到 "sdrawkcab"。
public static int reverseWord(String word) {
int palindromes = 0;
for (int i = word.length(); i >= 0; i--) {
System.out.print(i);
word.equalsIgnoreCase();
if (word.charAt(i)) == index(word.charAt(0 && 1))) {
palindromes++
System.out.println(palindromes)
}
return i;
}
}
下面的代码打印输入字符串的反转并检查它是否是回文
public static void main(String[] args) {
String input = "dad";
char temp[] = input.toCharArray();//converting it to a array so that each character can be compared to the original string
char output[] = new char[temp.length];//taking another array of the same size as the input string
for (int i = temp.length - 1, j = 0; i >= 0; i--, j++) {//i variable for iterating through the input string and j variable for inserting data into output string.
System.out.print(temp[i]);//printing each variable of the input string in reverse order.
output[j] = temp[i];//inserting data into output string
}
System.out.println(String.valueOf(output));
if (String.valueOf(output).equalsIgnoreCase(input)) {//comparing the output string with the input string for palindrome check
System.out.println("palindrome");
}
}
您的代码存在多个问题。
1.The equalsIgnoreCase 的原型是
public boolean equalsIgnoreCase(String str);
所以这个方法期望传递一个字符串,但是你没有传递任何东西 here.To 解决这个问题,传递另一个你想要匹配你的 word 的字符串像这样..
word.equalsIgnoreCase("myAnotherString");
2.word.charAt(i);
假设word="qwerty",那么每个字符的索引都会像这样
/* q w e r t y
0 1 2 3 4 5 */
所以当你使用i = word.length();
我会是6,因为单词的长度是6.So
word.charAt(i)
将在索引 6 处搜索字符,但由于没有索引 6,它将 return 一个异常 ArrayIndexOutOfBound。要解决此问题,请开始 i from word.length()-1
.
3.if (word.charAt(i));
This extra " ) ".Remove it
.
Index() 是你自己的方法吗?如果是,那么也检查一下。
因为您关于代码有什么问题的问题已经在这里得到解答,您可以通过使用一些比直接使用字符数组低一些的概念来实现这一点
public static boolean printWordAndCheckIfPalindrome(final String word) {
// Create a StringBuilder which helps when building a string
final StringBuilder reversedWordBuilder = new StringBuilder("");
// Get a stream of the character values of the word
word.chars()
// Add each character to the beginning of the reversed word,
// example for "backwards": "b", "ab", "cab", "kcab", ...
.forEach(characterOfString -> reversedWordBuilder.insert(0, (char) characterOfString));
// Generate a String out of the contents of the StringBuilder
final String reversedWord = reversedWordBuilder.toString();
// print the reversed word
System.out.println(reversedWord);
// if the reversed word equals the given word it is a palindrome
return word.equals(reversedWord);
}
我是 java 的新手,我写了这个方法来输入一个字符串单词并输出反向拼写的单词。目的是创建 一种方法,而不是使用已经存在的方法,例如简单的reverse。请帮我指明如何反转单词的方向。如果有回文,我也在尝试 determine/count。请帮忙!我已经阅读了其他问题,但找不到足够具体的内容来说明我的情况。我知道我的代码没有 运行,但我不确定如何修复它以获得正确的输出。
一个例子是 "backwards" 转到 "sdrawkcab"。
public static int reverseWord(String word) {
int palindromes = 0;
for (int i = word.length(); i >= 0; i--) {
System.out.print(i);
word.equalsIgnoreCase();
if (word.charAt(i)) == index(word.charAt(0 && 1))) {
palindromes++
System.out.println(palindromes)
}
return i;
}
}
下面的代码打印输入字符串的反转并检查它是否是回文
public static void main(String[] args) {
String input = "dad";
char temp[] = input.toCharArray();//converting it to a array so that each character can be compared to the original string
char output[] = new char[temp.length];//taking another array of the same size as the input string
for (int i = temp.length - 1, j = 0; i >= 0; i--, j++) {//i variable for iterating through the input string and j variable for inserting data into output string.
System.out.print(temp[i]);//printing each variable of the input string in reverse order.
output[j] = temp[i];//inserting data into output string
}
System.out.println(String.valueOf(output));
if (String.valueOf(output).equalsIgnoreCase(input)) {//comparing the output string with the input string for palindrome check
System.out.println("palindrome");
}
}
您的代码存在多个问题。
1.The equalsIgnoreCase 的原型是
public boolean equalsIgnoreCase(String str);
所以这个方法期望传递一个字符串,但是你没有传递任何东西 here.To 解决这个问题,传递另一个你想要匹配你的 word 的字符串像这样..
word.equalsIgnoreCase("myAnotherString");
2.word.charAt(i);
假设word="qwerty",那么每个字符的索引都会像这样
/* q w e r t y
0 1 2 3 4 5 */
所以当你使用i = word.length();
我会是6,因为单词的长度是6.So
word.charAt(i)
将在索引 6 处搜索字符,但由于没有索引 6,它将 return 一个异常 ArrayIndexOutOfBound。要解决此问题,请开始 i from word.length()-1
.
3.if (word.charAt(i));
This extra " ) ".Remove it
.
Index() 是你自己的方法吗?如果是,那么也检查一下。
因为您关于代码有什么问题的问题已经在这里得到解答,您可以通过使用一些比直接使用字符数组低一些的概念来实现这一点
public static boolean printWordAndCheckIfPalindrome(final String word) {
// Create a StringBuilder which helps when building a string
final StringBuilder reversedWordBuilder = new StringBuilder("");
// Get a stream of the character values of the word
word.chars()
// Add each character to the beginning of the reversed word,
// example for "backwards": "b", "ab", "cab", "kcab", ...
.forEach(characterOfString -> reversedWordBuilder.insert(0, (char) characterOfString));
// Generate a String out of the contents of the StringBuilder
final String reversedWord = reversedWordBuilder.toString();
// print the reversed word
System.out.println(reversedWord);
// if the reversed word equals the given word it is a palindrome
return word.equals(reversedWord);
}