提问人:Abdelrhaman Nianaa 提问时间:10/22/2023 更新时间:10/22/2023 访问量:33
Madlibs 游戏的代码,如何存储每个输入而不是仅存储最后一个输入{JAVA}
Code for a Madlibs game, how to store each input instead of only the last input{JAVA}
问:
所以基本上这是我下面的代码,要求用户根据字符串输入一个名词或动词,直到字符串完成,但是当它输出编辑的字符串时,它会输出最后一个动词和用户输入的最后一个名词。我的问题是,有了这段代码,有没有办法将每个输入保存在字符串中的相应位置,然后移动到下一个输入? ps:我不被允许使用数组。
import java.util.Scanner;
public class MadLibsProject
{
public static void story()
{
Scanner in = new Scanner(System.in);
String noun = "";
String verb = "";
String story = "The Little Noun And The Old Noun By Shel Silverstein \nSaid the little boy, "+
"sometimes I drop my noun. \nSaid the little old man, I verb that too.\nThe little boy whispered, I verb my noun.\n"+
"I do too, laughed the old noun. \nSaid the little boy, I often cry. \nThe old "+
"man nodded. So do I. \nBut worst of all, verb the noun, "+
"it seems grown-ups don’t pay attention to me. \nAnd he felt the warmth of a "+
"wrinkled old hand. \nI verb what you verb, said the little old noun.";
String update ="";
String noun1 = noun;
String verb1 = verb;
for(int i =0;i< story.length()-4;i++)
{
String madlib = story.substring(i,i+4);
if("noun".contains(madlib) || ("Noun".contains(madlib)))
{
System.out.println("Please enter a noun");
noun = in.next();
}
else if("verb".contains(madlib))
{
System.out.println("Please enter a verb");
verb = in.next();
verb1 = verb;
}
noun1 = noun;
verb1 = verb;
update = store(noun1,verb1);
}
System.out.print(update);
}
public static String store(String noun,String verb)
{
// for int i = 0;i < story.length()-4; i++
// madlib = story.substring(i,i+4)
// noun.contains mmadlib
String story = "The Little "+ noun+" And The Old "+noun+" By Shel Silverstein \nSaid the little boy, "+
"sometimes I drop my "+noun+". \nSaid the little old man, I "+verb+" that too.\nThe little boy whispered, I "+verb+" my "+noun+".\n"+
"I do too, laughed the old "+noun+". \nSaid the little boy, I often cry. \nThe old "+
"man nodded. So do I. \nBut worst of all, "+verb+" the "+noun+", "+
"it seems grown-ups don’t pay attention to me. \nAnd he felt the warmth of a "+
"wrinkled old hand. \nI "+verb+" what you "+verb+", said the little old "+noun+".";
return story;
}
public static void main(String[] args)
{
story();
}
这是我现在的输出: 输入: 请输入名词 名词1 请输入名词 名词2 请输入名词 名词3 请输入动词 动词1 请输入动词 动词2 请输入名词 名词4 请输入名词 名词5 请输入动词 动词3 请输入名词 名词6 请输入动词 动词4 请输入动词 动词5 请输入名词 名词7 输出: 小名词7 和老名词7 谢尔·西尔弗斯坦 (Shel Silverstein) 小男孩说,有时我会去掉我的名词7。 小老头说,我也动词了5。 小男孩低声说,我是动词5我的名词7。 我也是,笑了老名词7。 小男孩说,我经常哭。 老人点了点头。我也是。 但最糟糕的是,动词 5 名词 7,似乎大人不注意我。 他感受到了一只布满皱纹的老手的温暖。 我动词5你动词5,说小老名词7。
your text
答:
-2赞
Reilas
10/22/2023
#1
"...有没有办法将每个输入保存在字符串中各自的位置,然后移动到下一个输入?..."
为名词和动词创建代码,例如“%noun%”或“=verb=”。
The Little =Noun= And The Old =Noun= By Shel Silverstein \nSaid the little boy,
sometimes I drop my =noun=. \nSaid the little old man, I =verb= that too.\nThe little boy whispered, I =verb= my =noun=.\n
I do too, laughed the old =noun=. \nSaid the little boy, I often cry. \nThe old
man nodded. So do I. \nBut worst of all, =verb= the =noun=,
it seems grown-ups don’t pay attention to me. \nAnd he felt the warmth of a
wrinkled old hand. \nI =verb= what you =verb=, said the little old =noun=.
然后,利用 StringBuilder#replace 方法添加值。
下面是一个示例。
Scanner in = new Scanner(System.in);
StringBuilder s = new StringBuilder(story);
String t;
for (int i = 0, j, n = s.length(); i < n; i++)
if (s.charAt(i) == '=') {
j = s.indexOf("=", i + 1);
System.out.printf("Please enter a %s%n", s.substring(i + 1, j));
s.replace(i, j + 1, t = in.next());
i += t.length();
n = s.length();
}
此外,您可能需要检查它是否是大写的。
Scanner in = new Scanner(System.in);
StringBuilder s = new StringBuilder(story);
String t;
boolean u;
for (int i = 0, j, n = s.length(); i < n; i++)
if (s.charAt(i) == '=') {
j = s.indexOf("=", i + 1);
t = s.substring(i + 1, j);
u = Character.isUpperCase(t.charAt(0));
System.out.printf("Please enter a %s%n", t.toLowerCase());
t = in.next();
if (u) t = t.substring(0, 1).toUpperCase() + t.substring(1);
s.replace(i, j + 1, t);
i += t.length();
n = s.length();
}
上一个:无法访问类
下一个:Java 编码马尔可夫矩阵
评论
java.util.List
List
String
equalsIgnoreCase