gpt4 book ai didi

java - 需要帮助在 JAVA 中反向打印单词

转载 作者:行者123 更新时间:2023-12-01 19:58:08 25 4
gpt4 key购买 nike

我在 JAVA 中遇到问题。我试图让字符串向后打印每个单词。

示例:Hello World 将是“olleh dlrow”

下面是我的类(class)。谢谢您的帮助。

public class ReadReverse {

String message = "Hello nice to meet you in CS102";
String[] myStrings = message.split(" "); //Makes an array of the words split up by spaces
String result = "";

{
// gets the length of the array and splits it up by each word
for (int i = 0; i < myStrings[i].length(); i++) {
message.push(myStrings[i].at(i));
// sets th length to the length of the word
int length = message.length();
// pops the letter out into the new stack
for (i = 0; i < length; i++) {
result += myStrings;
}
// checks to see if it is at the end of the string.
if (i != message.length() - 1) {
result += " ";
}

}
}
}

最佳答案

您可以通过多种方式实现字符串反转。

看看这个:

 class ReverseString
{
public static void main(String[] args)
{
String input = "Hello world";
String[] words = input.split(" ");
String reversedString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(input);
System.out.println(reversedString);

}

}

关于java - 需要帮助在 JAVA 中反向打印单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48789446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com