关于JAVA的随机数问题!(急!!!!!!!!!!!!!!!!!!!!!)

zhonggeng 2003-12-31 09:55:14
我想要随机地向一个文件写入数据,比如说:可能间隔10秒会向文件写入一些数据,紧接着下一次向文件写入数据的时间间隔可能是50秒,我希望向文件写入数据的时间间隔是随机的,该怎么做?谢谢!
...全文
55 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainfl 2003-12-31
  • 打赏
  • 举报
回复
试一下吧,看可不可以,你刷新文件的操作可以放在Refresh()函数中
我运行成功了,在按钮上显示的是刷新时间

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class Frame1 extends JFrame
{

public ReplyTimeThread rtTread;
JButton jBtn = new JButton();

public Frame1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setBounds(200,100,300,400);
rtTread = new ReplyTimeThread(this);
jBtn.setBounds(new Rectangle(113, 103, 117, 43));
jBtn.setText("show Random");
this.setLocale(java.util.Locale.getDefault());
this.getContentPane().setLayout(null);
rtTread.start();
this.getContentPane().add(jBtn, null);

}
public void Refresh(int intNum)
{
jBtn.setText(intNum/1000+" second");
}

public static void main(String[] args)
{
Frame1 fm = new Frame1();
fm.setVisible(true);
fm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}

}

class ReplyTimeThread extends Thread
{
Frame1 me;
public int randomNum;
Random randomTest = new Random();
public ReplyTimeThread(Frame1 me)
{
this.me = me;
}

public void run()
{
while(true)
{
try
{
randomNum = randomTest.nextInt();
randomNum = randomNum /100000;
me.Refresh(randomNum);
Thread.sleep( randomNum );
} catch (Exception e) {}
}
}

}
Leemaasn 2003-12-31
  • 打赏
  • 举报
回复
是啊。
用随机数控制线程的休眠时间嘛



将“流氓无赖”测试到底。。。
——始于2003年7月


树欲止而风不停,行云流水匆匆去;
树梢蚂蚱凭空望,江边浪花碎巨石;



支持“流金岁月”!!!
发送框,少个“右键菜单,选择粘贴”;
——2003年12月24日am

^@^
scj123456 2003-12-31
  • 打赏
  • 举报
回复
同意楼上的
famousun 2003-12-31
  • 打赏
  • 举报
回复
我的想法是线程控制时间,时间的生成可以用随机类Random

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧