2015-03-30 118 views
-5

所以,我有一个控制台的Java应用程序。它有一个菜单和几个子菜单。 一个随机消息(我已经有消息列表中,因此需要选择其中之一)在随机时间中显示,只有当用户在“主菜单”,没有子菜单。随机时间的随机消息

不要求你给我一个代码,只是算法。

回答

0

您正在寻找如何实现这一目标的逻辑?这里有一些 伪代码来帮助你开始

On.ChangeMenu{ 
    if(currentmenu=='main menu'){ 
     start_random_message_timer(); 
    } 
    else{ 
     stop_random_message_timer(); 
    } 
} 

function start_random_message_timer(){ 
    Thread timerthread=new Thread(function(){ 
     int minseconds=30; 
     int maxseconds=200; 
     int timeouttime=1000; 
     int randomtime=rand(minseconds,maxseconds); 
     int currenttime=time(); 
     int nexttime=currenttime+randomtime; 
     timeouttime=currentime+timeouttime; 
     while(currenttime<timeouttime){ 
      while(currenttime<nexttime){ 

         //this is the time wasting loop 
         currenttime=time(); 
       } 
      //here currenttime just passed nexttime 
      var message=selectrandommessagefromarrayofmessages(); 
      show(message); 
      randomtime=rand(minseconds,maxseconds); 
      nexttime=currenttime+randomtime; 
     } 
    }); 
    timerthread.Start(); 
} 

function stop_random_message_timer(){ 
    var currenttimerthread=gettimerthread(); 
    currenttimerthread.stop(); 
}