我想用每個單詞的字符向後打印句子。將每個單詞翻譯成句子
例如: 狐狸跳過柵欄。
成爲: ehT xf depmuj revo eht ecnef。
我只能顛倒第一個單詞,然後它給了我一個ArrayIndexOutOfBoundsException。
我想用每個單詞的字符向後打印句子。將每個單詞翻譯成句子
例如: 狐狸跳過柵欄。
成爲: ehT xf depmuj revo eht ecnef。
我只能顛倒第一個單詞,然後它給了我一個ArrayIndexOutOfBoundsException。
一些變化需要做你的代碼:
1.更改sentence.length()
與words.length
2.使用read.nextLine();
來獲得句子輸入
嘗試
import java.util.Scanner;
import java.util.Stack;
public class test {
public static void main(String[] args) {
Scanner read = new Scanner(System.in); // Error in line 11
System.out.print("Enter a sentence: ");
String sentence = read.nextLine();
boolean containsPeriod= false;
if (".".equals(sentence.substring(sentence.length()-1,sentence.length()))){
sentence = sentence.substring(0,sentence.length()-1);
containsPeriod=true;
}
String[] words = sentence.split(" ");
for (int i = 0; i < words.length; i++) {
String word = words[i]; // Error in line 23
System.out.print(reverseWord(word));
if(i!=words.length-1)
System.out.print(" ");
}
if(containsPeriod)
System.out.print(".");
read.close();
}
private static String reverseWord(String sentence) {
Stack<Character> rev = new Stack();
for (int ii = 0; ii < sentence.length(); ii++) {
rev.push(sentence.charAt(ii));
}
String out = "";
while (rev.size() != 0) {
out += rev.pop();
}
return out;
}
}
請嘗試下面的簡單代碼。這是很容易:
String[] words = sentence.split(" ");
for (String word : words) {
System.out.print(new StringBuilder(word).reverse() + " ");
}
這是一個很好的答案。 +1。 downvoters可以解釋他們的問題嗎? –
通常,當問及這些問題時,預期受訪者提供算法方法。儘管提到兩種方法都很好! – xploreraj
在第一for
循環替換sentence.length()
與words.length
。
你想在句子中每個單詞迭代一次這個循環;但是你寫的代碼在句子中的每個字母上迭代一次。那是你的錯誤。
試試這個
public static void main(String args[]) {
String input = "The fox jumped over the fence";
StringBuilder sbReverseSentance = new StringBuilder();
String words[] = input.split(" ");
for (String word : words) {
for (int i = word.length() - 1; i >= 0; i--) {
sbReverseSentance.append(word.charAt(i));
}
sbReverseSentance.append(" ");
}
System.out.println (sbReverseSentance.toString());
}
試試這個,它不處理標點符號。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = read.nextLine();
String[] words = sentence.split(" ");
String[] reversed_words = new String[words.length];
for (int i = 0; i < words.length; i++) {
reversed_words[i] = reverseWord(words[i]);
}
for (int i = 0; i < reversed_words.length; i++)
System.out.println(reversed_words[i]);
}
private static String reverseWord(String word) {
String result = "";
char[] array = word.toCharArray();
for (int i = array.length - 1; i >= 0; i--) {
result += array[i];
}
return result;
}
}
您可以修復你的代碼如下所示
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class Query1 {
public static void main(String[] args) {
Scanner read = new Scanner(System.in); // Error in line 11
System.out.print("Enter a sentence: ");
// String sentence = read.next();
// read full line here instead of just one word
String sentence = read.nextLine();
String[] words = sentence.split(" ");
// this looks goes to array length
// for (int i = 0; i < sentence.length(); i++) {
for (int i = 0; i < words.length; i++) {
String word = words[i]; // Error in line 23
// System.out.println(reverseWord(word));
// use print not println
System.out.print(reverseWord(word));
// this loop is not at all required
// for (i = 0; i < words.length - 1; i++) {
// System.out.println(" ");
// }
System.out.print(" ");
}
}
private static String reverseWord(String sentence) {
Deque<Character> rev = new ArrayDeque<Character>();
// ArrayStack<Character> rev = new ArrayStack<Character>();
for (int ii = 0; ii < sentence.length(); ii++) {
rev.push(sentence.charAt(ii));
}
String out = "";
while (rev.size() != 0) {
out += rev.pop();
}
return out;
}
}
歡迎來到SO!還有一個類似的問題。稍後您可以投票結束。祝你好運! –
應該有每個單詞* *在句子中,標題是太誤導 – Evgeny