gpt4 book ai didi

java - 如何替换字符串消息中的单词?

转载 作者:行者123 更新时间:2023-12-02 01:26:45 24 4
gpt4 key购买 nike

我已经尝试了一些不同的代码一段时间了。当我尝试替换该单词时,提示我的替换代码错误。如何替换消息中的所有单词?

public class StorySynthesiser {

public static void main(String[] args) {

String message;
String name ="Tiger";
String result = message.replaceAll("Hare", name);


message = "";
message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
message = message +"All the animals in the forest gathered to watch.\n";
message = message +"The Hare ran down the road for a while and then paused to rest. \n";
message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";
message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";



JOptionPane.showMessageDialog(null, message);

最佳答案

您首先要替换,然后赋值,并且还在 DialogPane 中显示其他变量,因此您需要执行以下操作:

    String name ="Tiger";


message = "";
message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
message = message +"All the animals in the forest gathered to watch.\n";
message = message +"The Hare ran down the road for a while and then paused to rest. \n";
message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";
message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";

String result = message.replaceAll("Hare", name);

JOptionPane.showMessageDialog(null, result);

关于java - 如何替换字符串消息中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56767783/

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