java编写程序:使得该程序可以将一段英文句子中的每个单词里的字母顺序颠倒,例如 “to be or not to be ” 变为 “ot eb ro ton ot eb”
时间: 2024-02-12 16:06:23 浏览: 31
以下是Java程序实现:
```java
import java.util.Scanner;
public class ReverseWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一段英文句子:");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
StringBuilder reverse = new StringBuilder(word);
reverse.reverse();
result.append(reverse).append(" ");
}
System.out.println("颠倒顺序后的句子为:" + result.toString().trim());
}
}
```
运行程序后,输入一段英文句子,程序将输出颠倒每个单词字母顺序后的句子。例如,输入 "to be or not to be",程序将输出 "ot eb ro ton ot eb"。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)