Java 编写一个每分钟执行不同操作的方法
我正在制作一个程序,在某个点上无限点击。我使用以下代码将这些单击设置为随机:Java 编写一个每分钟执行不同操作的方法,java,click,awtrobot,Java,Click,Awtrobot,我正在制作一个程序,在某个点上无限点击。我使用以下代码将这些单击设置为随机: public void leftClick() { int no = random.nextInt(5) + 1; robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(50 * no); robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.delay(250 * no);
public void leftClick() {
int no = random.nextInt(5) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(250 * no);
}
下面是使用leftClick()方法的循环(整数o在程序中初始化):
当在我的程序中实现时,这种“点击”会持续不断,每次点击之间会随机暂停。我对它进行了测试,结果是每分钟点击35-45次。有没有办法让我的程序在一分钟内点击35次,然后在下一分钟点击70-80次?要获得特定范围内的随机数,您可以执行以下操作: 给定范围[a,b](从a到b,包括a和b),执行以下操作:
interval=b-a+1
randnumvalue=从[0,0,1.0]中获取随机数
result=a+间隔*随机值
要处理不同的范围,请保存开始时间并确定您所处的“分钟”,然后选择适当的范围来应用上述伪代码。您可以执行以下操作
leftClick(boolean flag){
int no = random.nextInt(10)
if(flag == false){
no = 35+ no; // This will always give you a number between 35 and 45
flag = true;
}else{
no = 70 + no;
}
}
因此,每次使用布尔值true调用此方法时,在35和45之间得到no,否则70-80,您可以执行以下操作:
public void leftClick(int noOfClicksPerMinute) {
if(noOfClicksPerMinute >35 && noOfClicksPerMinute < 45){
int no = random.nextInt(35) + 10;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);//change and set appropriate delay
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(250 * no);//change and set appropriate delay
}
else{
int no = random.nextInt(70) + 10;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);//change and set appropriate delay
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(250 * no);//change and set appropriate delay
}
public void leftClick(int noOfClicksPerMinute){
if(noOfClicksPerMinute>35&&noOfClicksPerMinute<45){
int no=随机。nextInt(35)+10;
机器人鼠标按键(InputEvent.BUTTON1_掩码);
robot.delay(50*no);//更改并设置适当的延迟
robot.mouseRelease(InputEvent.BUTTON1_掩码);
robot.delay(250*no);//更改并设置适当的延迟
}
否则{
int no=随机。nextInt(70)+10;
机器人鼠标按键(InputEvent.BUTTON1_掩码);
robot.delay(50*no);//更改并设置适当的延迟
robot.mouseRelease(InputEvent.BUTTON1_掩码);
robot.delay(250*no);//更改并设置适当的延迟
}
这里有一个选项:
public class Foo {
long startTime = 0;
long lastMinCount = 1;
int multiplier = 50;
public void leftClick() {
long currentTime = System.currentTimeMillis();
if (startTime==0) {
startTime = currentTime;
}
else {
if (currentTime / startTime > lastMinCount) {
lastMinCount = currentTime / startTime;
multiplier = 10 * (random.nextInt(5) + 1);
}
}
int no = random.nextInt(5) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(multiplier * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(4 * multiplier * no);
}
}
你可以试试这个我想你也可以把延迟时间间隔设置为随机的。@shree.pat18延迟时间间隔已经是随机的,每次都会乘以一个随机数。@LastMind我用更多的代码编辑了原始帖子。你是说
如果(noOfClicksPerMinute>35&&noOfClicksPerMinute<45){
它仍然不起作用。它倾向于每分钟增加一次,这是不可取的,而且几乎总是在80次点击左右。当然,你可以减少乘数或无变量的范围。乘数最高为50次,因此我会惊讶地发现,你看到的点击次数始终比原来的多。Al因此,请注意,第二个延迟比第一个延迟短,即在200*不,而不是250*noOk,我现在就试试。只是想一想,startTime不是应该等于0吗?是的,只是一个输入错误。当然,你也可以让乘数的计算更精细。比如if(random.nextInt(5)<2){multiplier=10*(random.nextInt(5)+1);}否则{multiplier=floor(50/(random.nextInt(5)+1))}
public class Foo {
long startTime = 0;
long lastMinCount = 1;
int multiplier = 50;
public void leftClick() {
long currentTime = System.currentTimeMillis();
if (startTime==0) {
startTime = currentTime;
}
else {
if (currentTime / startTime > lastMinCount) {
lastMinCount = currentTime / startTime;
multiplier = 10 * (random.nextInt(5) + 1);
}
}
int no = random.nextInt(5) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(multiplier * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(4 * multiplier * no);
}
}