处理:将一串随机单词转换为单个句子

时间:2015-02-02 18:27:59

标签: string processing

我正在尝试在Processing中生成伪随机格式化句子。

我发现很多示例草图将一个句子分成一个单词列表,但没有一个将单独的单词合并成一个句子。

例如,我有没有办法将字符串单词[n]转换成单个句子?

String[] Space = {
" " };
String[] Verbs = { 
"like", "kill", "taste", "save" };
String[] PossessiveAdjectives = { 
"your", "her", "his", "my" };
String[] Nouns = { 
"apple", "bank", "cat", "dog" };

void setup(){
size(800,400);
background(0);
textAlign(CENTER);
textSize(100);
new_sentence();
}
void draw(){
}
void write_word(String[] words){
int n = int(random(words.length));
print(words[n]);
text(words[n], 0.5*width ,0.5*height+25);
}

void new_sentence(){
write_word(Verbs);
write_word(Space);
write_word(PossessiveAdjectives);
write_word(Space);
write_word(Nouns); 
}

void mousePressed(){
setup(); 
}

1 个答案:

答案 0 :(得分:0)

解决:

它现在包含String Sentence中随机选择的单词,并将它们与+“”+

组合
int screenWidth = 800;
int screenHeight = 400;

void setup() {
  size(screenWidth, screenHeight);
  sentence();
}

void sentence() {
  String[] Verbs = { 
    "like", "kill", "taste", "save"
  };
  String[] PossessiveAdjectives = { 
    "your", "her", "his", "my"
  };
  String[] Nouns = { 
    "apple", "bank", "cat", "dog"
  };

  int verb = int(random(Verbs.length));
  int possessiveAdjective = int(random(PossessiveAdjectives.length));
  int noun = int(random(Nouns.length));

  String Sentence = Verbs[verb]+" "+PossessiveAdjectives[possessiveAdjective]+" "+Nouns[noun];
  println(Sentence);

  background(0);
  textAlign(CENTER);
  textSize(100);
  text(Sentence, 0.5*screenWidth, 0.5*screenHeight+25);
}

void draw() {
}

void mousePressed() {
  sentence();
}