微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我单击鼠标时,如何使它们重新排列并具有不同的输出?

如何解决当我单击鼠标时,如何使它们重新排列并具有不同的输出?

该项目的目标是制作MadLib。我希望每次单击鼠标时数组都可以随机播放,我该怎么做?

   01 PROGRAM-VARIABLES.                        
      05 CL-CMD PIC X(33)                       
                VALUE "STRQSH CMD('LS')".
      05 PACK-VAL PIC 9(10)V9(5) COMP-3         
                  VALUE 16.                                    
   MAINLINE.                                    
       CALL "QCMDEXC" USING CL-CMD PACK-VAL.  

解决方法

您用mousePressed编写的内容将修改原始madLib字符串,并用新信息替换所有以散列分隔的参数。第一次运行时效果很好,并且打印的字符串替换了这些参数,所以您获得了正确的输出。

但是现在madLib字符串不包含任何哈希分隔的参数,因此,当mousePressed再次运行时,所有的replace命令都不会执行任何操作,因为它们替换的哈希分隔的参数不再存在。

因此,您需要修改字符串的副本,并对副本进行替换,并保留原始字符串。感谢@GeorgeProfenza提供此代码示例:

String madLib = "I went to #place# with #person#.  I know #person# from when we #event#.  At #place# we saw #thing#.  It was #adjective# so we ran away to #otherPlace#. ";

String[] array1 = {"Pakistan","The Pentagon","Universal Studios Sound Stage No. 5","The basement of the United States Embassy in Iran","Uncle Randy's Attic"};
String[] array2 = {"Former President Bill Clinton","Ten Dolla Founding Fatha Alexander Hamilton","Former Bufffalo Bills Running Back Orenthal James 'OJ' Simpson ","Clifford From Clifford The Big Red Dog","Director and 2 time oscar winner Quentin Tarantino "};
String[] array3 = {"Stole the entire contents of Fort Knox","Taught John D. Rockefeller how to get oil","Tried to but failed to prevent the Cold War","Tried to save Brandon Lee","Attemted to rob a Boost Mobile and got locked in"};
String[] array4 = {"A taxidermied lion with three legs ","cryogenically frozen Walt Disney","A below average height but above average speed brown colored horse","A bucket of warm cow milk","A replica of political podcaster Ben Shapiro"};
String[] array5 = {"Barking","Running around","Eating the Travis Scott meal from McDonalds","Watching Season 1 of Mickeymouse Clubhouse","Verbally abusing a cardboard cutout of Air Buddy for the Air Bud movies"};
String[] array6 = {"A random house","the museum of cats","The dog adoption center","The Wendys drivethrough line","The window and light fixture store"};

void setup() {
}
void draw() {
}
void mousePressed() {
  int index1 = int(random(array1.length));  
  int index2 = int(random(array2.length));  
  int index3 = int(random(array3.length));  
  int index4 = int(random(array4.length));  
  int index5 = int(random(array5.length));
  int index6 = int(random(array6.length));  

  String madLibResult = madLib.replace("#place#",(array1[index1]));
  madLibResult  = madLibResult.replace("#person#",(array2[index2]));   
  madLibResult  = madLibResult.replace("#event#",(array3[index3]));
  madLibResult  = madLibResult.replace("#thing#",(array4[index4]));   
  madLibResult  = madLibResult.replace("#adjective#",(array5[index5]));
  madLibResult  = madLibResult.replace("#otherPlace#",(array6[index6]));

  println(madLibResult);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。