所以,我有一个控制台java应用程序。它有一个菜单和几个子菜单。 随机消息(我已经有消息列表,因此需要选择其中一个)才能在随机时间显示,仅当用户在&#时34;主菜单" ,而不是子菜单。
不要求你给我一个代码,只是算法。
答案 0 :(得分: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();
}