所以基本上这是我下面的代码,要求用户根据字符串输入名词或动词,直到字符串完成为止,但是当它输出编辑后的字符串时,它会输出最后一个动词和最后一个动词用户输入的名词。我的问题是,使用这段代码,有没有办法将每个输入保存在字符串中各自的位置,然后移动到下一个? 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。 小老头说,我也这么说。 小男孩低声说,我是动词,我是名词。 我也是,笑了老名词7。 小男孩说,我经常哭。 老者点了点头。我也是。 但最糟糕的是,动词5名词7,似乎大人不注意我。 他感受到了一只布满皱纹的老手的温暖。 我动词5你动词5,说的是小老名词7。
your text
为名词和动词创建代码,例如"%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 方法添加值。
这是一个例子。
import static java.lang.System.out;
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);
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));
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();
}